2012-07-12 50 views
1

我簡單地序列化使用.NET XmlSerializer將Items作爲集合的Person;XmlSerializer:格式化標記

class Item 
{ 
    Name 
    Price 
} 

class Person 
{ 
    Name 
    List Items<Item> 
} 

Everthing罰款...我使用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#)中執行它?

+0

裝飾你Name & Price性這個問題是與此類似(即序列化的屬性時,對元素的行爲):http://stackoverflow.com/questions/11449564/why-是-A-場在-AN-自動生成的類序列化 - 進入 - 一個元件 - 當界定 – Anton 2012-07-12 11:19:12

回答

0

XmlAttribute

2
class Item 
{ 
    [System.Xml.Serialization.XmlAttributeAttribute("name")] 
    string Name; 
    [System.Xml.Serialization.XmlAttributeAttribute("price")] 
    string Price; 
}