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 ? )
<results>
<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 country="Albania">
<Regions region="Centralna Albania">
<Provinces province="Durres i okolice">
<Cities city="Durres" cityCode="2B66E0ACFAEF78734E3AF1194BFA6F8DEC4C5760">
<IndividualFlagsWithForObjects storage="0" Status="1" />
<IndividualFlagsWithForObjects storage="1" Status="0" />
<IndividualFlagsWithForObjects storage="2" Status="1" />
</Cities>
</Provinces>
</Regions>
</Countries>
</results>
I must point one important thing: the parent node is but when I use it loaded.Descendants("results") it gives me nothing.
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.Attribute("ountry").Value,
Region = (string)c.Element("Regions").Attribute("region").Value,
Province= c.Element("Regions").Element("Provinces").Attribute("prowincja").Value,
City= c.Element("Regions").Element("Provinces").Element("Cities").Attribute("city").Value,
Kod = c.Element("Regions").Element("Provinces").Element("Cities").Attribute("cityCode").Value,
IndywidualnaFlagaStatus = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("Status"),
IndywidualnaFlagaWartosc = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("storage")
}).ToList();
last problem:
IndywidualnaFlagaWartosc = c.Element("Regions").Element("Provinces").Element("Cities").Element("IndividualFlagsWithForObjects").Attribute("storage")
gives me :
IndywidualnaFlagaWartosc = {storage="0"} (I see this while debugging)