我使用.NET XmlSerializer简单地序列化有项目作为集合的Person;
class Item
{
Name
Price
}
class Person
{
Name
List Items<Item>
}一切都很好……我使用XmlWriterSettings来缩进我的xml文件。输出为:
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<name>TestName</name>
<Items>
<Item>
<name>one</name>
<price>0</price>
</Item>
<Item>
<name>two</name>
<price>1</price>
</Item>
</Items>
</Viewport>但我想要的是:
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<name>TestName</name>
<Items>
<Item name="one" price="0" />
<Item name="two" price="1" />
</Items>
</Viewport>很快而不是
<Item>
<name>one</name>
<price>0</price>
</Item>我想把xml写成
<Item name="one" price="0" />如何在.NET(C#)中完成?
发布于 2012-07-12 19:21:00
使用XmlAttribute装饰您的Name和Price属性
发布于 2012-07-12 20:19:41
class Item
{
[System.Xml.Serialization.XmlAttributeAttribute("name")]
string Name;
[System.Xml.Serialization.XmlAttributeAttribute("price")]
string Price;
}https://stackoverflow.com/questions/11450671
复制相似问题