public static class XmlSerializer
{
public static void SaveToXml(string filePath, object sourceObj, Type type, string xmlRootName)
{
if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null)
{
type = type != null ? type : sourceObj.GetType();
using (StreamWriter writer = new StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xmlSerializer = string.IsNullOrWhiteSpace(xmlRootName) ?
new System.Xml.Serialization.XmlSerializer(type) :
new System.Xml.Serialization.XmlSerializer(type, new XmlRootAttribute(xmlRootName));
xmlSerializer.Serialize(writer, sourceObj);
}
}
}
public static object LoadFromXml(string filePath, Type type)
{
object result = null;
if (File.Exists(filePath))
{
using (StreamReader reader = new StreamReader(filePath))
{
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
result = xmlSerializer.Deserialize(reader);
}
}
return result;
}
}
[XmlRootAttribute("MyCity", Namespace="abc.abc", IsNullable=false)] // 当该类为Xml根节点时,以此为根节点名称。
public class City
[XmlAttribute("AreaName")] // 表现为Xml节点属性。<... AreaName="..."/>
public string Name
[XmlElementAttribute("AreaId", IsNullable = false)] // 表现为Xml节点。<AreaId>...</AreaId>
public string Id
[XmlArrayAttribute("Areas")] // 表现为Xml层次结构,根为Areas,其所属的每个该集合节点元素名为类名。<Areas><Area ... /><Area ... /></Areas>
public Area[] Areas
[XmlElementAttribute("Area", IsNullable = false)] // 表现为水平结构的Xml节点。<Area ... /><Area ... />...
public Area[] Areas
[XmlIgnoreAttribute] // 忽略该元素的序列化。
[XmlRootAttribute("MyCity", Namespace = "abc.abc", IsNullable = false)]
public class City
{
[XmlAttribute("CityName")]
public string Name
{
get;
set;
}
[XmlAttribute("CityId")]
public string Id
{
get;
set;
}
[XmlArrayAttribute("Areas")]
public Area[] Areas
{
get;
set;
}
}
[XmlRootAttribute("MyArea")]
public class Area
{
[XmlAttribute("AreaName")]
public string Name
{
get;
set;
}
[XmlElementAttribute("AreaId", IsNullable = false)]
public string Id
{
get;
set;
}
[XmlElementAttribute("Street", IsNullable = false)]
public string[] Streets
{
get;
set;
}
}
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 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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有