2010-08-05 71 views
12

上午傢伙,C#的Xml序列化列表<T>後裔與XML屬性

我有一個從名單下降,具有公共屬性的集合。 Xml序列化程序不會提取我的proeprty。列表項目序列化很好。我已經嘗試過XmlAttribute屬性無濟於事。你們有解決方案嗎?

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 
    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     var people = new PersonCollection 
     { 
      new Person { FirstName="Sue", Age=17 }, 
      new Person { FirstName="Joe", Age=21 } 
     }; 
     people.FavoritePerson = "Sue"; 

     var x = new XmlSerializer(people.GetType()); 
     var b = new StringBuilder(); 
     var w = XmlTextWriter.Create(b, new XmlWriterSettings { NewLineChars = "\r\n", Indent = true }); 
     x.Serialize(w, people); 
     var s = b.ToString(); 
    } 
} 

[XmlRoot(ElementName="People")] 
public class PersonCollection : List<Person> 
{ 
    //DOES NOT WORK! ARGHHH 
    [XmlAttribute] 
    public string FavoritePerson { get; set; }  
} 

public class Person 
{ 
    [XmlAttribute] 
    public string FirstName { get; set; } 
    [XmlAttribute] 
    public int Age { get; set; } 
} 

我得到下面的XML

<?xml version="1.0" encoding="utf-16"?> 
     <People xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
      <Person FirstName="Sue" Age="17" /> 
      <Person FirstName="Joe" Age="21" /> 
     </People> 

我想獲得這個

<?xml version="1.0" encoding="utf-16"?> 
     <People FavoritePerson="Sue" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
      <Person FirstName="Sue" Age="17" /> 
      <Person FirstName="Joe" Age="21" /> 
     </People> 

回答

4

我繼續通過實施IXmlSerializable的解決了這個問題。如果存在更簡單的解決方案,請發佈!

[XmlRoot(ElementName="People")] 
public class PersonCollection : List<Person>, IXmlSerializable 
{ 
    //IT WORKS NOW!!! Too bad we have to implement IXmlSerializable 
    [XmlAttribute] 
    public string FavoritePerson { get; set; } 

    public System.Xml.Schema.XmlSchema GetSchema() 
    { 
     return null; 
    } 
    public void ReadXml(XmlReader reader) 
    { 
     FavoritePerson = reader[0];    
     while (reader.Read()) 
     { 
      if (reader.Name == "Person") 
      { 
       var p = new Person(); 
       p.FirstName = reader[0]; 
       p.Age = int.Parse(reader[1]); 
       Add(p); 
      } 
     } 
    } 
    public void WriteXml(XmlWriter writer) 
    { 
     writer.WriteAttributeString("FavoritePerson", FavoritePerson); 
     foreach (var p in this) 
     { 
      writer.WriteStartElement("Person"); 
      writer.WriteAttributeString("FirstName", p.FirstName); 
      writer.WriteAttributeString("Age", p.Age.ToString()); 
      writer.WriteEndElement();    
     } 
    } 
} 
+0

史蒂夫,你可以接受你自己的答案。 – nawfal 2014-07-16 20:44:20

4

這不是問題的答案,但我想我會提出一個建議,以緩解代碼開發。

PersonCollection類添加一個新的Add方法,例如:

public class PersonCollection : List<Person>, IXmlSerializable 
{ 
... 
    public void Add(string firstName, int age) 
    { 
     this.Add(new Person(firstName, age)); 
    } 
... 
} 

然後,通過這樣做,可以簡化您的集合初始化語法:

var people = new PersonCollection 
{ 
    { "Sue", 17 }, 
    { "Joe", 21 } 
}; 
people.FavoritePerson = "Sue"; 
+0

酷招。謝謝! – Steve 2010-08-06 18:54:46

1

如果你不這樣做介意不得不包裝所有的列表函數,那麼你可以將列表作爲類的屬性嵌入,而不是從列表中導出。

然後,您將使用XmlElement屬性強制將xml元素寫出爲平面列表(而不是嵌套)。