1、XML Attribute
[Serializable] 将该类标记为可以序列化类
XmlElement 节点重命名
XmlRoot 根节点重名称
XmlArray List集合添加根节点
XmlArrayItem List集合中子节点重命名
[XmlAttribute(“attr”)] 此属性会作为特性在 元素中
[XmlIgnore] 此特性是忽略此属性
这里用简单的城市,区域和街区作为例子,具体示范一下上面的规则:
[XmlRoot("MyCity", Namespace = "abc.abc", IsNullable = false)]//当该类为Xml根节点时,以此为根节点名称。
public class City
{
[XmlAttribute("CityName")]
public string Name
{
get;
set;
}
[XmlAttribute("CityId")]
public string Id
{
get;
set;
}
// 表现为Xml层次结构,根为Areas,其所属的每个该集合节点元素名为类名。<Areas><Area ... /><Area ... /></Areas>
[XmlArrayAttribute("Areas")]
public Area[] Areas
{
get;
set;
}
[XmlIgnoreAttribute]//忽略该元素的序列化。
}
[XmlRoot("MyArea")]
public class Area
{
[XmlAttribute("AreaName")]//表现为Xml节点属性。<... AreaName="..."/>
public string Name
{
get;
set;
}
[XmlElement("AreaId", IsNullable = false)]// 表现为Xml节点并重命名。<AreaId>...</AreaId>
public string Id
{
get;
set;
}
[XmlElement("Street", IsNullable = false)]
public string[] Streets
{
get;
set;
}
}
根据以上类型,我们mock一些数据:
static void Main(string[] args)
{
Area area1 = new Area();
area1.Name = "Pudong";
area1.Id = "PD001";
area1.Streets = new string [] { "street 001", "street 002" };
Area area2 = new Area();
area2.Name = "Xuhui";
area2.Id = "XH002";
area2.Streets = new string [] { "street 003", "street 004" };
City city1 = new City();
city1.Name = "Shanghai";
city1.Id = "SH001";
city1.Areas = new Area[] { area1, area2 };
XmlSerializer.SaveToXml(@"C:\temp\XML\output003.xml", city1);
}
最终输出的XML为:
<?xml version="1.0" encoding="utf-8"?>
<MyCity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
CityName="Shanghai" CityId="SH001" xmlns="abc.abc">
<Areas>
<Area AreaName="Pudong">
<AreaId>PD001</AreaId>
<Street>street 001</Street>
<Street>street 002</Street>
</Area>
<Area AreaName="Xuhui">
<AreaId>XH002</AreaId>
<Street>street 003</Street>
<Street>street 004</Street>
</Area>
</Areas>
</MyCity>
2、XML 序列化与反序列化
public class XMLExtensions
{
/// <summary>
/// 对象序列化为XML字符串
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="t">Model实体对象</param>
/// <returns></returns>
public static string Serialize<T>(T t)
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(sw, t);
return sw.ToString();
}
}
/// <summary>
/// 将一个对象序列化为XML字符串。这个方法将不生成XML文档声明头。
/// </summary>
/// <param name="o">要序列化的对象</param>
/// <returns>序列化产生的XML字符串</returns>
public static string XmlSerializerObject(object o)
{
Encoding encoding = Encoding.UTF8;
XmlSerializer serializer = new XmlSerializer(o.GetType());
using (MemoryStream stream = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars = "\r\n";
settings.Encoding = encoding;
settings.OmitXmlDeclaration = true;
settings.IndentChars = " ";
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, o, ns);
writer.Close();
}
//return Encoding.UTF8.GetString(stream.ToArray());
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream, encoding))
{
return reader.ReadToEnd();
}
}
}
/// <summary>
/// XML反序列化为对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="xml">xml字符串</param>
/// <returns></returns>
public static T Deserialize<T>(string xml)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
using StringReader stringReader = new StringReader(xml);
return (T)xmlSerializer.Deserialize(stringReader);
}
}
标签:XML,string,street,new,格式,序列化,public From: https://www.cnblogs.com/xixi-in-summer/p/18233010