user1931853
user1931853

Reputation: 61

Deserialize XML in a WP8 Application

I'm trying to develop a Windows phone 8 app (I'm new in wp8 dev).

I have an XML file that look like this:


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

This is my Quotes class:

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

This is the quote class:

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

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

Then I use this code to deserialize it:

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;

        }  

This work fine in every feed having this structure with one <text> section. But I have feed with a lot of <text>

If I use C# code above, only first <text> section is parsed, others are ignored. I need to create separate List or ObservableCollection for each <text> section in single XML feed.

Upvotes: 2

Views: 1747

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

Change your Quote class to contain List<string> text instead of string text:

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

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

Update

Because of existing functionality within your app and current Quote class members I would leave serialization and use LINQ to XML to load data from XML into Quotes class instance:

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()
};

I've tested it with following Quotes and Quote class declarations:

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

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

    public string text { get; set; }
}

Attributes are no longer necessary because this approach does not use XmlSerialization.

Upvotes: 1

Related Questions