本文介绍了使用序列化C#嵌套XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用序列化创建嵌套的Xml。我有两个数据列表必须生成由Number过滤的嵌套xml。
列表:
I need to create nested Xml with serialization. I have two Lists with data that have to generate nested xml filtered by Number.
The lists:
List<Person> personList = new List<Person>();
personList.Add(new Person{
Number = 1,
Name = "Dean"
});
personList.Add(new Person{
Number = 2,
Name = "Mike"
});
List<Home> homeList= new List<Home>();
homeList.Add(new Home{
Number = 2,
City= "Paris",
State = "France"
});
homeList.Add(new Home{
Number = 1,
City= "London",
State = "England"
});
接下来我有用于序列化的类:
So next i have class that i use for serialization:
public class CreateXML
{
[XElement(ElementName = "Home")]
List<Home> homeList= new List<Home>();
[XElement(ElementName = "Person")]
List<Person> personList = new List<Person>();
}
创建XML的方法:
Method for creating XML:
public void Serialize(CreateXML list)
{
XmlSerializer serializer = new XmlSerializer(typeof(CreateXML));
using (TextWriter writer = new StreamWriter(@"D:\XmlTEST.txt"))
{
serializer.Serialize(writer, list);
}
}
现在它首先使用所有Person数据生成Xml文件,然后生成Home数据。 />
通缉输出:
Now it generates Xml file first with all Person data then with Home data.
Wanted ouput:
<Person>
<number>1<number>
<name>Dean</name>
<Home>
<number>1</number>
<city>London</city>
<state>England</state>
</Home>
</Person>
<Person>
<number>2<number>
<name>Mike</name>
<Home>
<number>2</number>
<city>Paris</city>
<state>France</state>
</Home>
</Person>
任何建议?
谢谢。
Any suggestions?
Thanks.
推荐答案
这篇关于使用序列化C#嵌套XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!