net中序列化读写xml

参考http://www.cnblogs.com/fish-li/archive/2013/05/05/3061816.html

我们可以直接使用XmlTextReader、XmlDocument、XPath来取数XML中的数据, 也可以使用LINQ TO XML或者反序列化的方法从XML中读写数据,我各人比较喜欢序列化、反序列化方法。

1、默认情况下,不添加任何属性标签的对象属性都序列化为一个节点。如下一个Student对象序列化结果如下:

public class Student
{
public string Name { get; set; }//属性无任何修饰都序列化为Element public string Sex { get; set; } public int Age { get; set; }
}
Student stu = new Student()
{
Name = "LiLei",
Age = ,
Sex = "男"
};
string xml= XmlHelper.XmlSerialize(stu, Encoding.UTF8);//序列化

序列化结果如下:

<?xml version="1.0" encoding="utf-8"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>LiLei</Name>
<Sex>男</Sex>
<Age>25</Age>
</Student>

2、使用XmlAttribute修饰类属性,则对应属性序列化为节点属性

using System.Xml.Serialization;

namespace Entity
{
public class Student
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Sex { get; set; }
[XmlAttribute]
public int Age { get; set; }
}
} Student stu = new Student()
{
Name = "LiLei",
Age = ,
Sex = "男"
};
string xml= XmlHelper.XmlSerialize(stu, Encoding.UTF8);

序列化结果:

<!--被XmlAttribute修饰的类属性都序列化为Xml节点属性-->
<?xml version="1.0" encoding="utf-8"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="LiLei" Sex="男" Age="25" />

3、使用XmlText修饰为xml节点InnerText

public class Student
{
[XmlText]
public string Name { get; set; } [XmlAttribute]
public string Sex { get; set; } [XmlAttribute]
public int Age { get; set; }
} Student stu = new Student()
{
Name = "LiLei",
Age = ,
Sex = "男"
};
string xml= XmlHelper.XmlSerialize(stu, Encoding.UTF8);

序列化结果

<?xml version="1.0" encoding="utf-8"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Sex="男" Age="25">
  LiLei
</Student>

4、重命名

属性:[XmlAttribute("别名")]

元素节点:[XmlElement("别名")]

列表元素节点:[XmlArrayItem("别名")]

列表元素自身:[XmlArray("别名")]

类型即根节点:[XmlType("别名")]或[XmlRoot("别名")]

[XmlType("Stu")]
public class Student
{
[XmlElement("Name")]//重命名为Name
public string StudentName { get; set; } [XmlAttribute("Sex")]//重命名为Sex
public string StudentSex { get; set; } [XmlAttribute]
public int Age { get; set; }
}
Student stu = new Student()
{
StudentName = "LiLei",
Age = ,
StudentSex = "男"
};
string xml= XmlHelper.XmlSerialize(stu, Encoding.UTF8);

序列化结果:

<Stu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Sex="男" Age="25">
<Name>LiLei</Name>
</Stu>

5、列表和数组在序列

例如:
public class Student
{
[XmlElement("Name")]
public string StudentName { get; set; } [XmlAttribute("Sex")]
public string StudentSex { get; set; } [XmlAttribute]
public int Age { get; set; }
}
List<Student> stu_list = new List<Student>() {
new Student() { StudentName = "Lily", Age = , StudentSex = "女" },
new Student() { StudentName = "Hanmeimei", Age = , StudentSex = "女" },
new Student() { StudentName = "Linda", Age = , StudentSex = "女" },
}; string xml = XmlHelper.XmlSerialize(stu_list, Encoding.UTF8);

序列化xml结果为:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Student Sex="女" Age="19">
<Name>Lily</Name>
</Student>
<Student Sex="女" Age="21">
<Name>Hanmeimei</Name>
</Student>
<Student Sex="女" Age="19">
<Name>Linda</Name>
</Student>
</ArrayOfStudent>

该结果的根节点显然不大友好,为此可以重新定义一个新类型,例如:

[XmlRoot("Students")]
public class StudentList : List<Student> { } StudentList stulist = new StudentList() {
new Student() { StudentName = "Lily", Age = , StudentSex = "女" },
new Student() { StudentName = "Hanmeimei", Age = , StudentSex = "女" },
new Student() { StudentName = "Linda", Age = , StudentSex = "女" }
}; string xml = XmlHelper.XmlSerialize(stulist, Encoding.UTF8);

序列化为Xml结果如下:

<?xml version="1.0" encoding="utf-8"?>
<Students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Student Sex="女" Age="19">
<Name>Lily</Name>
</Student>
<Student Sex="女" Age="21">
<Name>Hanmeimei</Name>
</Student>
<Student Sex="女" Age="19">
<Name>Linda</Name>
</Student>
</Students>

6、类成员为数组或列表的序列化

注意:数组和列表都在序列化时,默认情况下会根据类型中的数据成员名称生成一个节点, 列表项会生成子节点,如果要重命名,可以使用[XmlArrayItem]和[XmlArray]来实现。 还可以直接用[XmlElement]控制不生成列表的父节点

例如:

 public class QueClient
{
public string IP { get; set; } public int LocalPort{ get; set; } public List<Queue> Queues { get; set; } } public class Queue
{
[XmlAttribute("Name")]
public string QueueName { get; set; }
[XmlAttribute("Id")]
public string QueueId { get; set; }
[XmlAttribute("Prefix")]
public string QuePrefix { get; set; } } List<Queue> quelist = new List<Queue>()
{
new Queue() {QueueName="金卡",QueueId="A",QuePrefix="A" },
new Queue() {QueueName="普卡",QueueId="B",QuePrefix="B" },
new Queue() {QueueName="葵花卡",QueueId="C",QuePrefix="C" },
};
QueClient queClient = new QueClient() {
IP ="99.12.53.71",
LocalPort =,
Queues =quelist
}; string xml = XmlHelper.XmlSerialize(queClient, Encoding.UTF8);

序列化结果:

<?xml version="1.0" encoding="utf-8"?>
<QueClient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<IP>99.12.53.71</IP>
<LocalPort>88</LocalPort>
<Queues>
<Queue Name="金卡" Id="A" Prefix="A" />
<Queue Name="普卡" Id="B" Prefix="B" />
<Queue Name="葵花卡" Id="C" Prefix="C" />
</Queues>
</QueClient>

假如QueClient成员Queues(是列表类型)机该列表节点名需重名,即Queues节点与Queue节点需要重名,只需修改QueueClient,如下:

  public class QueClient
{
public string IP { get; set; } public int LocalPort{ get; set; } [XmlArrayItem("Que")]//重命名列表节点名称
[XmlArray("Ques")]//重命名列表根节点名称
public List<Queue> Queues { get; set; } } List<Queue> quelist = new List<Queue>()
{
new Queue() {QueueName="金卡",QueueId="A",QuePrefix="A" },
new Queue() {QueueName="普卡",QueueId="B",QuePrefix="B" },
new Queue() {QueueName="葵花卡",QueueId="C",QuePrefix="C" },
};
QueClient queClient = new QueClient() {
IP ="99.12.53.71",
LocalPort =,
Queues =quelist
}; string xml = XmlHelper.XmlSerialize(queClient, Encoding.UTF8);

序列化结果如下:

<?xml version="1.0" encoding="utf-8"?>
<QueClient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<IP>99.12.53.71</IP>
<LocalPort>88</LocalPort>
<Ques>
<Que Name="金卡" Id="A" Prefix="A" />
<Que Name="普卡" Id="B" Prefix="B" />
<Que Name="葵花卡" Id="C" Prefix="C" />
</Ques>
</QueClient>

7、类继承与反序列化

列表元素可以是同一种类型,也可以不是同一种类型(某个类型的派生类)

例如:思考如何反序列化

<?xml version="1.0" encoding="utf-8"?>
<XRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<List>
<X1 aa="1" bb="2" />
<X1 aa="3" bb="4" />
<x2>
<cc>ccccccccccccccccc</cc>
<dd>dddddddddd</dd>
</x2>
</List>
</XRoot>

思想:列表里不同类型的继承同一个父类且同时为列表成员指定多个[XmlArrayItem(typeof(XXX))]可实现多种派生类型混在一起输出。最后反序列化结果如下:

using System;
using System.Collections.Generic;
using System.Xml.Serialization; namespace Entity
{
public class XBase { } [XmlRoot("x1")]
public class X1 : XBase
{
[XmlAttribute("aa")]
public String AA { get; set; }
[XmlAttribute("bb")]
public string BB { get; set; } } [XmlType("x2")]
public class X2 : XBase
{
[XmlElement("cc")]
public string CC { get; set; }
[XmlElement("dd")]
public string DD { get; set; }
} public class XRoot
{
[XmlArrayItem(typeof(X1))]//注意此处指出节点类型两个X1,X2
[XmlArrayItem(typeof(X2))]//注意此处指出节点类型两个X1,X2
public List<XBase> List { get; set; }
}
}

例如:反序列化如下xml内容:

<DynamicHelp xmlns="http://msdn.microsoft.com/vsdata/xsd/vsdh.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://msdn.microsoft.com/vsdata/xsd/vsdh.xsd vsdh.xsd">
<LinkGroup ID="sites" Title="Venus Sites" Priority="1500">
<Glyph Collapsed="3" Expanded="4"/>
</LinkGroup>
<LinkGroup ID="Venus Private Forums" Title="Venus Private Forums" Priority="1400">
<Glyph Collapsed="3" Expanded="4"/>
</LinkGroup>
<LinkGroup ID="ASP.NET Forums" Title="ASP.NET 1.0 Public Forums" Priority="1200">
<Glyph Collapsed="3" Expanded="4"/>
</LinkGroup>
<Context>
<Links>
<LItem URL="http://www.asp.net/venus" LinkGroup="sites">Venus Home Page</LItem>
<LItem URL="http://www.asp.net" LinkGroup="sites">ASP.NET Home Page</LItem>
<LItem URL="http://www.asp.net/Forums/ShowForum.aspx?tabindex=1&amp;ForumID=77"
LinkGroup="Venus Private Forums">General Discussions</LItem>
<LItem URL="http://www.asp.net/Forums/ShowForum.aspx?tabindex=1&amp;ForumID=83"
LinkGroup="Venus Private Forums">Feature Requests</LItem>
<LItem URL="http://www.asp.net/Forums/ShowForum.aspx?tabindex=1&amp;ForumID=78"
LinkGroup="Venus Private Forums">Bug Reports</LItem>
<LItem URL="http://www.asp.net/Forums/ShowForum.aspx?tabindex=1&amp;ForumID=86"
LinkGroup="Venus Private Forums">ASP.NET 2.0 Related issues</LItem>
<LItem URL="http://www.asp.net/Forums/ShowForum.aspx?tabindex=1&amp;ForumID=11"
LinkGroup="ASP.NET Forums">Announcements</LItem>
<LItem URL="http://www.asp.net/Forums/ShowForum.aspx?tabindex=1&amp;ForumID=15"
LinkGroup="ASP.NET Forums">Getting Started</LItem>
<LItem URL="http://www.asp.net/Forums/ShowForum.aspx?tabindex=1&amp;ForumID=18"
LinkGroup="ASP.NET Forums">Web Forms</LItem>
</Links>
</Context>
</DynamicHelp>

反序列化为:

using System.Collections.Generic;
using System.Xml.Serialization; namespace Entity
{
[XmlRoot(Namespace = "http://msdn.microsoft.com/vsdata/xsd/vsdh.xsd")]//注意此处与xml内容一致,xml内容有空间限定时类定义也需添加
public class DynamicHelp
{
[XmlElement]//注意此处
public List<LinkGroup> Groups { get; set; } public Context Context { get; set; }
} public class LinkGroup {
[XmlAttribute]
public string ID { get; set; }
[XmlAttribute]
public string Title { get; set; }
[XmlAttribute]
public int Priority { get; set; } public Glyph Glyph { get; set; }
} public class Glyph
{
[XmlAttribute]
public int Collapsed { get; set; }
[XmlAttribute]
public int Expanded { get; set; }
} public class LItem
{
[XmlAttribute]
public string URL { get; set; }
[XmlAttribute]
public string LinkGroup { get; set; } [XmlText]
public string Title { get; set; }
} public class Context {
public List<LItem> Links { get; set; }
}
}

8、不需要序列化的属性: [XmlIgnore]

 public class QueClient
{
public string IP { get; set; }
public int LocalPort{ get; set; }
[XmlIgnore]
public int LocalPort2{ get; set; }
[XmlElement]
public List<Queue> Queues { get; set; } }

9、指定类成员序列化顺序[XmlElement(Order<= 顺序数字)]

例如:

  public class QueClient
{
[XmlElement(Order =)]//设置序列化顺序
public string IP { get; set; } public int LocalPort{ get; set; } [XmlIgnore]
public int LocalPort2{ get; set; } [XmlElement]
public List<Queue> Queues { get; set; } }

10、序列化去掉命名空间及声明

 private static void XmlSerializeInternal(Stream stream,object obj,Encoding encoding)
{
if (obj == null)
throw new ArgumentNullException("object");
if(encoding==null)
throw new ArgumentNullException("object");
XmlSerializer serializer = new XmlSerializer(obj.GetType()); XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars = "\r\n";
settings.Encoding = encoding;
settings.IndentChars = " ";
//设置忽略声明空间
settings.OmitXmlDeclaration = true;
// 强制指定命名空间,覆盖默认的命名空间。
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty); using (XmlWriter writer=XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, obj,namespaces);
}
}

11、自定义序列化行为

假如如下代码序列化:

    public class TestClass
{
public string StrValue { get; set; } public List<int> List { get; set; }
} public class ClassB1
{
public TestClass Test { get; set; }
} TestClass test = new TestClass {
StrValue = "Fish Li",
List = new List<int> { , , , , }
};
ClassB1 b1 = new ClassB1 { Test = test };
string xmbll = XmlHelper.XmlSerialize(b1, Encoding.UTF8);

序列化结果:

<ClassB1>
<Test>
<StrValue>Fish Li</StrValue>
<List>
<int>1</int>
<int>2</int>
<int>3</int>
<int>4</int>
<int>5</int>
</List>
</Test>
</ClassB1>

但实际需求是序列化为如下:

<ClassB1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Test s="Fish Li">1,2,3,4,5</Test>
</ClassB1>

此时只需要对需要特殊序列化的类进行重定义,即该类实现IXmlSerializable重写ReadXml与WriteXml方法

public class TestClass: IXmlSerializable
{
public string StrValue { get; set; } public List<int> List { get; set; } public XmlSchema GetSchema()
{
return null;
} public void ReadXml(XmlReader reader)
{
StrValue = reader.GetAttribute("s");
string numbers = reader.ReadString();
if(string.IsNullOrWhiteSpace(numbers)==false)
List = (from s in numbers.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries)
let n = int.Parse(s)
select n).ToList();
} public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("s", StrValue);
writer.WriteString(string.Join(",", List.ConvertAll<string>(x => x.ToString()).ToArray()));
}
}
上一篇:ACM/ICPC 之 DFS范例(ZOJ2412-ZOJ1008)


下一篇:Properties类的用法