English 中文(简体)
IEnumerable<XElement>和foreach
原标题:IEnumerable<XElement> and foreach

我想选择

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);
 }

如何输出所有元素?我对迭代器有一些问题。

I don t now how to iterate a IEnumerable with foreach and access the element.Name property.

//***广告信息//

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>

我必须将这个XML文件导入到一个标准化的MySQL表中。ARTCOMP/ARTBAR是MySQL数据库中的附加表。

首先,我想在一个空的MySQL表中将所有字段创建为varchar()。另一个问题是,并非每个ART元素都有相同的子元素。也许有一种更好的方法可以找到所有可能的子元素(某种模式)。

最佳回答

您的select子句意味着q的类型实际上是IEnumerable<;IEnumerable<;XElement>>。我怀疑你的意思是:

var q = from artikel in xmlSource.Descendants("ART")
        where artikel.Element("ID").Value.Equals("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;

或者甚至:

var q = from artikel in xmlSource.Descendants("ART")
        where (int) artikel.Element("ID") == 15
        from element in artikel.Elements()
        select element;

这将给出q的类型,其仅为IEnumerable<;XElement>-它基本上会使结果变平。您将得到一个所有元素的序列,这些元素直接位于一个名为“ART”的元素之下,该元素又有一个值为15的“ID”元素。

如果这不是您想要的,请提供更多信息——最好是一个示例XML文件以及您期望的输出。

您似乎不太可能真的想要打印所有元素的元素名称。。。客户名称应存储为的某个位置(元素内的文本或属性),而不是元素名称。

编辑:如果希望使用IEnumerable<;IEnumerable<;XElement>>您可以使用:

foreach (var result in q)
{
    Console.WriteLine("Next result...");
    foreach (var element in result)
    {
        Console.WriteLine("Got name: {0}", element.Name);
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...