Problem with linq-to-xml

Posted by phenevo on Stack Overflow See other posts from Stack Overflow or by phenevo
Published on 2010-05-07T12:17:53Z Indexed on 2010/05/07 12:28 UTC
Read the original article Hit count: 331

Filed under:
|
|

I want by linq save my xml in csv and I have o problem.

This bracket are here beacuse without it this code is not displaying (why ? )

bracket results bracket <Countries country="Albania"><Regions region="Centralna Albania"><Provinces province="Durres i okolice"><Cities city="Durres" cityCode="2B66E0ACFAEF78734E3AF1194BFA6F8DEC4C5760"><IndividualFlagsWithForObjects Status="1" /><IndividualFlagsWithForObjects  Status="0" /><IndividualFlagsWithForObjects magazyn="2" /></Cities></Provinces></Regions></Countries><Countries .... 

XDocument loaded = XDocument.Load(@"c:\citiesxml.xml");

                // create a writer and open the file
                TextWriter tw = new StreamWriter("c:\\XmltoCSV.txt");

                // Query the data and write out a subset of contacts
                var contacts = (from c in loaded.Descendants("Countries")
                               select new
                               {
                                   Country = (string)c.Element("Country"),
                                   Region = (string)c.Element("region"),
                                   Province= (string)c.Element("province"),
                                   City = (string)c.Element("city"),
                                   Hotel = (string)c.Element("hotel")
                               }).ToList();

Problem is that loaded.Descendants("Countries") gives me 45 countries but all fields are null.

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml