C# 用C编写包含多个对象的XML文档#
我班上有几个物体C# 用C编写包含多个对象的XML文档#,c#,xml,C#,Xml,我班上有几个物体 List<string> a List<string> b Dictionary<string,string> c 列表a 名单b 字典c 我想要一个结果XML文档,其中包含这三个对象中包含的数据,使其看起来像: <myobject> <as> <a value="xxxxx" /> <a value="xxxxx" /> <a value="xx
List<string> a
List<string> b
Dictionary<string,string> c
列表a
名单b
字典c
我想要一个结果XML文档,其中包含这三个对象中包含的数据,使其看起来像:
<myobject>
<as>
<a value="xxxxx" />
<a value="xxxxx" />
<a value="xxxxx" />
</as>
<bs>
<b value="xxxxx" />
<b value="xxxxx" />
<b value="xxxxx" />
</bs>
<cs>
<c key="x" value="xxxxx" />
<c key="x" value="xxxxx" />
<c key="x" value="xxxxx" />
</cs>
</myobject>
这里最合适的技术是什么?
我应该迭代每个对象还是有更简单的方法?
Tere是.Net中的一些XML编写者,我不确定该使用哪种。XML序列化是更好的方法吗?
XmlSerializer
如果不实现IXmlSerializable
或类似的功能,将不知道如何序列化字典。
using System.Linq;
using System.Xml.Linq;
...
XDocument doc = new XDocument(
new XElement("as",
a.Select(item => new XElement("a",
new XAttribute("value", item)
)
),
new XElement("bs",
b.Select(item => new XElement("b",
new XAttribute("value", item)
)
),
new XElement ("cs",
c.Select(item => new XElement("c",
new XAttribute("key", item.Key),
new XAttribute("value", item.Value)
)
)
);