2013-03-18 13 views
2

我正在嘗試開發Windows Phone 8應用程序(我是wp8開發中的新成員)。反序列化WP8應用程序中的XML

我有一個看起來像這樣的XML文件:


<?xml version="1.0" ?> 
<root> 
    <quotes> 
     <quote> 
     <author></author> 
     <text></text> 
     <text></text> 
     <text></text> 
     </quote> 
    </quotes> 
</root> 

這是我的行情類:

[XmlRoot("root")] 
public class Quotes 
{ 
    [XmlArray("quotes")] 
    [XmlArrayItem("quote")] 
    public ObservableCollection<Quote> Collection { get; set; } 
} 

這是引用類:

public class Quote 
{ 
    [XmlElement("author")] 
    public string author { get; set; } 

    [XmlElement("text")] 
    public string text { get; set; } 
} 

然後我用這個代碼反序列化:

XmlSerializer serializer = new XmlSerializer(typeof(Quotes)); 
XDocument document = XDocument.Parse(e.Result); 
Quotes quotes = (Quotes) serializer.Deserialize(document.CreateReader()); 
quotesList.ItemsSource = quotes.Collection; 

// selected Quote 
     Quote quote; 

     public QuotePage() 
     { 
      InitializeComponent(); 

      // get selected quote from App Class 
      var app = App.Current as App; 
      quote = app.selectedQuote; 

      // show quote details in page 
      author.Text = quote.author; 
      text.Text = quote.text; 

     } 

具有這種結構有一個<text>部分每飼料在此做工精細。但我有很多的飼料<text>

如果我使用上面的C#代碼,只有第一個<text>節被解析,其他人被忽略。我需要爲單個XML Feed中的每個<text>部分創建單獨的List或ObservableCollection。

+0

「這項工作很好,每個飼料有這種結構與一節」 - 你的意思是你有多個引號部分? – Hassan 2013-03-18 15:34:19

+0

顯示'報價'類代碼。 – MarcinJuraszek 2013-03-18 15:49:09

+0

如果您使用您喜歡的結構創建一個類並進行序列化,它會更容易。所以這個字符串將是你的xml所需要的。 – 2013-03-18 15:53:41

回答

1

更改Quote類包含List<string> text而不是string text

public class Quote 
{ 
    [XmlElement("author")] 
    public string author { get; set; } 

    [XmlElement("text")] 
    public List<string> text { get; set; } 
} 

更新

因爲你的應用程序中現有的功能和目前Quote類成員的我會離開系列化和使用LINQ到XML從XML加載數據到Quotes類實例:

XDocument document = XDocument.Parse(e.Result); 
Quotes quotes = new Quotes() { 
    Collection = document.Root 
         .Element("quotes") 
         .Elements("quote") 
         .Select(q => new { 
          xml = q, 
          Author = (string) q.Element("author") 
         }) 
         .SelectMany(q => q.xml.Elements("text") 
              .Select(t => new Quote() { 
               author = q.Author, 
               text = (string)t 
              })) 
         .ToList() 
}; 

我已經與以下QuotesQuote類的聲明進行了測試:

public class Quotes 
{ 
    public List<Quote> Collection { get; set; } 
} 

public class Quote 
{ 
    public string author { get; set; } 

    public string text { get; set; } 
} 

屬性不再是必需的,因爲這種方法不使用XmlSerialization。

+0

謝謝你的回答。它說不能隱式地將'System.Collections.Generic.List '轉換爲'字符串',所以我想我必須改變頁面中的實現,我將顯示文本,但我不知道如何做到這一點你能幫忙嗎?: 引用引用; (); public QuotePage() { InitializeComponent(); var app = App.Current as App; quote = app.selectedQuote; //在頁面顯示報價詳細信息 author.Text = quote.author; text.Text = quote.text; } – user1931853 2013-03-18 18:21:52

+0

您可能已經在該代碼的某個位置設置了該屬性的引用。你可以更新你的問題,並寫出你想從該XML實現什麼對象(以及它的內容)嗎?它是一個'Quote'類對象與作者和文本列表,或者'List '每個都有一個作者和一個XML文本? – MarcinJuraszek 2013-03-18 18:24:00

+0

我添加了代碼。非常感謝您的幫助 – user1931853 2013-03-18 18:30:02