1

I have a xml file like this:

<plist version="1.0">
<array>
    <dict>
        <key>Title</key>
        <string>Chapters</string>
        <key>Items</key>
        <array>
            <dict>
                <key>Title</key>
                <string>XYZ</string>

            </dict>
            <dict>
                <key>Title</key>
                <string>ABC</string>

            </dict>
              </array>
    </dict>
    <dict>
        <key>Title</key>
        <string>ChaptersONE</string>
        <key>Items</key>
        <array>
            <dict>
                <key>Title</key>
                <string>ASDF</string>

            </dict>
               </array>
    </dict>
</array>

I have a class like this:

class Chapter
{
    public string Title { get; set; }
    public string SubTitle { get; set; }
}

How do i parse this xml and assign to the Chapter as list ?

I have tried the below :

List<Chapters> List = (from d in doc.Root.Element("array").Elements("dict")
                       select new HighwayCode
                       {
                           Title = (string)d.Element("string"),
                           SubTitle = d.Element("array")
                                        .Elements("dict")
                                        .Elements("string")
                                        .Select(s => (string)s)
                                        .ToList()
                       }).ToList();

But here the SubTitle is a list but i need that as a string

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
user2056563
  • 600
  • 2
  • 12
  • 37
  • Maybe this can help : http://stackoverflow.com/questions/18498467/parent-children-xml-to-dto-object-model-with-linq – granadaCoder Feb 24 '14 at 18:11
  • ok post the full xml so i can copy paste it , and then describe what you want your output to be phrased as , take some time to make it clear ,because you question is barley readable, and i will help. – eran otzap Feb 24 '14 at 19:31

1 Answers1

1

You mean comma-delimited string? Use String.Join:

List<Chapters> List = (from d in doc.Root.Element("array").Elements("dict")
                       select new HighwayCode
                       {
                           Title = (string)d.Element("string"),
                           SubTitles = String.Join(",", d.Element("array")
                                                         .Elements("dict")
                                                         .Elements("string")
                                                         .Select(s => (string)s))
                       }).ToList();
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • This works, but in my UI it displays items like XYZ,ABC in LongListSelector but i need them one below the other – user2056563 Feb 24 '14 at 18:15
  • So you do need a list... and problem is you don't know how to bind it into `LongListSelector`... – MarcinJuraszek Feb 24 '14 at 18:16
  • @MarcinJuraszek can you please help me http://stackoverflow.com/questions/22146447/linq-to-xml-with-nested-dictionary-and-array#comment33605276_22146447 – Goofy Mar 03 '14 at 12:30
  • @MarcinJuraszek can you please help me here http://stackoverflow.com/questions/23290982/parse-xml-with-varying-key – user2056563 Apr 25 '14 at 10:52