使用序列化C#嵌套XML [英] Nested XML with Serialization C#

查看:99
本文介绍了使用序列化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.

推荐答案

我认为唯一合理的XML序列化技术是 Data Contract http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



如果你使用 XmlSerializer 来解决某些需要它的技术,我会理解的。更确切地说,您需要使用 System.Runtime.Serialization.ISerializable 以及该序列化程序所需的其他技术。但这对您的数据类型来说是笨拙,侵入性和限制性的。但是你试图直接使用这种道德上过时的串行器,这没有任何实际意义。使用 System.Runtime.Serialization.DataContractSerializer (它也是一个XML序列化程序),您不必实现任何接口或从任何特定类派生您的类型,不需要。合同由属性定义,这使得该技术完全非侵入性且易于使用。您只需要将任意代码图存储到任何流中的机制,然后将其恢复到内存中,就像序列化之前一样。



另见我过去的答案:

如何将数据合同从C#/ WCF代码传递给托管C ++ [ ^ ],

如何在表单应用程序中使用XML文件编写器和阅读器? [ ^ ],

Creati ng属性文件...... [ ^ ]。



-SA
I think the only reasonable XML serialization technology is Data Contract: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

I would understand if you used XmlSerializer for some technology which requires it. More exactly, you would need to use System.Runtime.Serialization.ISerializable and other techniques needed for that serializer. But this is awkward, intrusive and limiting to your data types. But you are trying to use this morally obsolete serializer directly, which makes no practical sense. With System.Runtime.Serialization.DataContractSerializer (which is also an XML serializer), you don't have to implement any interfaces or derive your types from any certain classes, nothing. The contract is defined by attributes, which makes the technology fully non-intrusive and easy to use. You simply have the mechanism to store any arbitrary code graph to any stream and later restore it in memory the way it was before serialization.

See also my past answers:
how to pass a datacontract from a C#/WCF code to Managed C++[^],
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


这篇关于使用序列化C#嵌套XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆