Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable<XElement> and foreach

I would like to select all elements of a

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 don't now how to iterate a IEnumerable with foreach and access the element.Name property.

//*** Aditional Information //

Example 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 have to import this XML File into a normalized MySQL Table. ARTCOMP / ARTBAR are additional tables in the MySQL Datebase.

As a 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. Maybe there is a better way of finding all possible child elements (kind of schema).

like image 688
Brainski Avatar asked Dec 17 '22 13:12

Brainski


1 Answers

Your select clause 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;

Or:

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;

That will give a type for q of just IEnumerable<XElement> - it will flatten the results, basically. You'll get a sequence of all elements which are directly beneath an element called "ART" which in turn has an "ID" element of with a value of 15.

If that's not what you're looking for, please give more 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 the elements though... the customer name should be stored as a value somewhere (either text within the element or an attribute), not as the element name.

EDIT: If you want to use an 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);
    }
}
like image 91
Jon Skeet Avatar answered Jan 05 '23 07:01

Jon Skeet