简体   繁体   English

Linq to XML动态XML后代

[英]Linq to XML dynamic XML Decendants

I'm parsing a lot of XML files using Linq to XML synatx, everything works when I try to access top level elements 我正在使用Linq到XML synatx解析许多XML文件,当我尝试访问顶级元素时,一切正常

var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot")
                        select new
                        {
                            model = (string)element.Element("MODEL"),
                        }).FirstOrDefault()

The problem occurs when I need to access lower level childs of that document I tried: 当我需要访问我尝试过的该文档的下级子级时,会发生问题:

var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot")
                        select new
                        {
                            ddName = (string)element.Descendants("DD_NAME").Elements("name").First();
                            }).FirstOrDefault()

and

var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot").Descendants("DD_NAME")
                             select new
                             {
                                 ddName = (string)element.Element("name")
                             }).FirstOrDefault();

Sadly none of that works and i get same error "Sequence contains no elements". 遗憾的是,这些都不起作用,并且我收到相同的错误“序列不包含任何元素”。 And one more thing sometimes the XML document contains those tags and sometimes not is something like this enough for handling this case? 还有一件事,有时XML文档包含这些标记,有时还不够,足以应付这种情况?

var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot").Descendants("DD_NAME")
                             select new
                             {
                                 ddName = (string)element.Element("name") ?? "-"
                             }).FirstOrDefault();

Edit: I don't think is possible to paste short version of XML that would be simple, so here's full version: http://pastebin.com/uDkP3rnR and for the code example: 编辑:我认为不可能粘贴简单的XML短版,所以这里是完整版: http : //pastebin.com/uDkP3rnR,并提供代码示例:

XDocument prodcutDocument = XDocument.Load(this.ServerPATHData + file); 

var indexroot = (from element in prodcutDocument.Root.Descendants("indexroot")
            select new
            {
                modelis = (string)element.Element("MODELIS"),
                T2918_0 = (string)element.Descendants("dd_DARBINIS_GRAFIKAS_SPEC").First()
            }).FirstOrDefault();

writeTxt.WriteLine("modelis: " + indexroot.modelis);
writeTxt.WriteLine("T2979_0" + indexroot.T2918_0); 

You can just change: 您可以更改:

element.Descendants("dd_DARBINIS_GRAFIKAS_SPEC").First()

to this: 对此:

element.Descendants("dd_DARBINIS_GRAFIKAS_SPEC").FirstOrDefault() ?? "-"

In examining the sample XML that you posted on PasteBin, it appears to me that the elements that you mention appear only once. 在检查您在PasteBin上发布的示例XML时,在我看来,您提到的元素仅出现一次。 To access them, you can simply specify a path to each as follows: 要访问它们,您只需指定一个路径即可,如下所示:

XElement indexroot = document.Root.Element("indexroot");
XElement modelis = indexroot.Element("MODELIS");
XElement dd_dgs = indexroot.Element("dd_DARBINIS_GRAFIKAS_SPEC");

XElement voltageuv = dd_dgs.Element("VoltageUV");

string t2979_0 = (string)voltageuv.Element("T2979_0");
string t2861_60 = (string)voltageuv.Element("T2861_60");
string t2757_121 = (string)voltageuv.Element("T2757_121");

(Note that you may need to check for null if there is a chance that any of the elements you are trying to access may not be present. Without doing so, you'll encounter a NullReferenceException .) (请注意,如果您尝试访问的任何元素可能不存在,则可能需要检查null 。如果不这样做,则会遇到NullReferenceException 。)

Here is a snippet of the XML that you posted to give context to the above code: 这是您发布的XML片段,用于提供上述代码的上下文:

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<PDB>
  <indexroot>
    <ed_BENDRA_MAKS_SUV_GALIA>1.45</ed_BENDRA_MAKS_SUV_GALIA>
    <ed_BENDRA_MAKS_SROVE>6.48</ed_BENDRA_MAKS_SROVE>
    <TIPAS>1</TIPAS>
    <MODELIS>RIS 2500 HW EC 3.0</MODELIS>
    <dd_DARBINIS_GRAFIKAS_SPEC>
      <VoltageUV>
        <T2979_0>229,42</T2979_0>
        <T2861_60>227,98</T2861_60>
        <T2757_121>228,97</T2757_121>
      </VoltageUV>
      <CurrentIA>
        <T2979_0>2,56</T2979_0>
        <T2861_60>2,63</T2861_60>
        <T2757_121>2,72</T2757_121>
      </CurrentIA>
    </dd_DARBINIS_GRAFIKAS_SPEC>
  </indexroot>
</PDB>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM