IEnumerable <XElement> and foreach
I would like to select all items
var q = from artikel in xmlSource.Descendants("ART") where artikel.Element("ID").Value.Equals("15") select artikel.Elements(); //Does not work foreach (var element in q) { Console.WriteLine("Customer name = {0}", element.Name); } How can I output all elements? I have some problems with iterators.
I do not now how iteratirovat IEnumerable with foreach and access to element.Name property.
// *** Additional Information //
Sample XML
<ARTICLE> <ART> <ID>0020209</ID> <EXP>36</EXP> <QTY>1</QTY> <SMCAT>B</SMCAT> <DSCRD>Example Description 1</DSCRD> <ARTCOMP> <COMPNO>10710</COMPNO> <ROLE>H</ROLE> <ARTNO1>320059</ARTNO1> <ARTNO2>320059</ARTNO2> </ARTCOMP> <ARTCOMP> <COMPNO>10710</COMPNO> <ROLE>V</ROLE> <ARTNO1>320059</ARTNO1> <ARTNO2>320059</ARTNO2> </ARTCOMP> <ARTBAR> <CDTYP>E13</CDTYP> <BC>7680202580475</BC> <BCSTAT>A</BCSTAT> </ARTBAR> <ARTPRI> <VDAT>2010-12-01T00:00:00+01:00</VDAT> <PTYP>PEXF</PTYP> <PRICE>30</PRICE> </ARTPRI> </ART> <ART> <ID>0020244</ID> <EXP>60</EXP> <QTY>30</QTY> <DSCRD>FERRO GRADUMET Depottabl 30 Stk</DSCRD> <ARTCOMP> <COMPNO>1836</COMPNO> <ROLE>H</ROLE> <ARTNO1>685230</ARTNO1> <ARTNO2>685230</ARTNO2> </ARTCOMP> <ARTCOMP> <COMPNO>1836</COMPNO> <ROLE>V</ROLE> <ARTNO1>685230</ARTNO1> <ARTNO2>685230</ARTNO2> </ARTCOMP> <ARTCOMP> <COMPNO>5360</COMPNO> <ROLE>L</ROLE> <ARTNO1>685230</ARTNO1> <ARTNO2>685230</ARTNO2> </ARTCOMP> </ART> </ARTICLE> I need to import this XML file into a normalized table MySQL. ARTCOMP / ARTBAR are additional tables in a MySQL database.
In the beginning, I wanted to create all the fields as varchar () in an empty MySQL table. As an additional problem, not every ART element has the same child elements. Perhaps there is a better way to find all possible child elements (type of scheme).
Your offer select means that the type of q is actually IEnumerable<IEnumerable<XElement>> . I suspect you mean:
var q = from artikel in xmlSource.Descendants("ART") where artikel.Element("ID").Value.Equals("15") from element in artikel.Elements() select element; "ART") var q = from artikel in xmlSource.Descendants("ART") where artikel.Element("ID").Value.Equals("15") from element in artikel.Elements() select element; Or:
var q = from artikel in xmlSource.Descendants("ART") where (string) artikel.Element("ID") == "15" from element in artikel.Elements() select element; "ART") var q = from artikel in xmlSource.Descendants("ART") where (string) artikel.Element("ID") == "15" from element in artikel.Elements() select element; ") == " var q = from artikel in xmlSource.Descendants("ART") where (string) artikel.Element("ID") == "15" from element in artikel.Elements() select element; or even:
var q = from artikel in xmlSource.Descendants("ART") where (int) artikel.Element("ID") == 15 from element in artikel.Elements() select element; "ART") var q = from artikel in xmlSource.Descendants("ART") where (int) artikel.Element("ID") == 15 from element in artikel.Elements() select element; This will give the type of q only IEnumerable<XElement> - it will smooth the results generally. You will get a sequence of all elements that are directly below the element called "ART", which in turn has an "ID" element with a value of 15.
If this is not what you are looking for, provide additional information - ideally a sample XML file along with your expected output.
It seems unlikely that you really want to print the element name of all elements, though ... the client name should be stored as a value somewhere (text inside the element or attribute), and not as the element name.
EDIT: If you want to use IEnumerable<IEnumerable<XElement>> , you can use:
foreach (var result in q) { Console.WriteLine("Next result..."); foreach (var element in result) { Console.WriteLine("Got name: {0}", element.Name); } } ; foreach (var result in q) { Console.WriteLine("Next result..."); foreach (var element in result) { Console.WriteLine("Got name: {0}", element.Name); } }