2012-01-11 45 views
2

我試圖反序列化下面的XML文檔:反序列化與空收集XMLSERIALIZE結果的集合

<?xml version="1.0" encoding="utf-8" ?> 
<TestPrice> 
    <Price> 
    <A>A</A> 
    <B>B</B> 
    <C>C</C>  
    <Intervals> 
     <Interval> 
     <A>A</A> 
     <B>B</B> 
     <C>C</C> 
     </Interval> 
     <Interval> 
     <A>A</A> 
     <B>B</B> 
     <C>C</C> 
     </Interval> 
    </Intervals> 
    </Price> 
</TestPrice> 

我有三個類定義爲反序列化爲一個對象圖這樣的:

public class TestPrice 
{ 
    private List<Price> _prices = new List<Price>(); 
    public List<Price> Price 
    { 
     get { return _prices; } 
     set { _prices = value; } 
    } 
} 

public class Price 
{ 
    public string A { get; set; } 
    public string B { get; set; } 
    public string C { get; set; } 

    private List<Interval> _intervals = new List<Interval>(); 
    public List<Interval> Intervals 
    { 
     get { return _intervals; } 
     set { _intervals = value; } 
    } 
} 

public class Interval 
{ 
    public string A { get; set; } 
    public string B { get; set; } 
    public string C { get; set; } 
} 

我可以反序列化每個部分。也就是說,我可以這樣做:

var serializer = new XmlSerializer(typeof(Price)); 
var priceEntity = ((Price)(serializer.Deserialize(XmlReader.Create(stringReader)))); 

而且priceEntity正確地包含在stringReader XML數據,包括List<Interval> Intervals初始化。但是,如果我試圖反序列化TestPrice實例,它總是會出現一個空的List<Price> Price

如果我改變的TestPrice這樣的定義:

public class TestPrice 
{ 
    public Price Price { get; set; } 
} 

它的工作原理。但當然我的XSD將Price定義爲一個序列。我有其他實體反序列化正確,但它們不包含根元素中的序列。有沒有我不知道的限制?我應該在TestPrice中包含某種元數據嗎?

回答

3

只是[XmlElement]裝點您的收藏價格:

[XmlElement(ElementName = "Price")] 
public List<Price> Price 
{ 
    get { return _prices; } 
    set { _prices = value; } 
} 

而且你似乎是反序列化Price,而在你的XML根標籤是TestPrice。所以,這裏是一個完整的例子:

public class TestPrice 
{ 
    [XmlElement(ElementName = "Price")] 
    public List<Price> Price { get; set; } 
} 

public class Price 
{ 
    public string A { get; set; } 
    public string B { get; set; } 
    public string C { get; set; } 

    public List<Interval> Intervals { get; set; } 
} 

public class Interval 
{ 
    public string A { get; set; } 
    public string B { get; set; } 
    public string C { get; set; } 
} 

class Program 
{ 
    static void Main() 
    { 
     var xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?> 
<TestPrice> 
    <Price> 
    <A>A</A> 
    <B>B</B> 
    <C>C</C> 
    <Intervals> 
     <Interval> 
     <A>A</A> 
     <B>B</B> 
     <C>C</C> 
     </Interval> 
     <Interval> 
     <A>A</A> 
     <B>B</B> 
     <C>C</C> 
     </Interval> 
    </Intervals> 
    </Price> 
</TestPrice>"; 

     var serializer = new XmlSerializer(typeof(TestPrice)); 
     using (var reader = new StringReader(xml)) 
     using (var xmlReader = XmlReader.Create(reader)) 
     { 
      var priceEntity = (TestPrice)serializer.Deserialize(xmlReader); 
      foreach (var price in priceEntity.Price) 
      { 
       // do something with the price 
      } 
     } 
    } 
} 
+0

我裝飾價格集合建議和執行你的例子。我仍然收回一個空的價格收集。順便說一句,爲什麼你將一個新的XmlReader實例傳遞給串行器,而不是你已經在xmlReader中的那個? – Cesar 2012-01-11 21:59:06

+0

@塞薩爾,對不起,這是讀者的錯誤。我已經修復了我的示例。它現在應該工作。 – 2012-01-11 22:00:08

+0

有趣,我運行你完全相同的代碼,它的工作原理。但是如果我將它適應於我的實際代碼,我仍然會得到相同的結果。無論如何,至少我知道它應該起作用,所以我將其標記爲已接受。謝謝! – Cesar 2012-01-11 22:14:22