基于不同属性嵌套LINQ to XML

时间:2013-10-09 16:54:52

标签: c# xml linq nested

我正在尝试编写嵌套查询,因为我正在使用的XML文档有多个子元素。 我需要访问document-id属性为“docdb”的值。 我需要访问document-id属性为“epodoc”的值。

我的班级案例_定义如下:

public class case_
{
public string appNumber { get; set; }
public string appDate { get; set; }
}

这是我到目前为止所做的:

var myCase = from theCases in allCasesXML.Descendants("exchange-document")
select new case_
{
appNumber = (string)theCases.Element("bibliographic-data").Element("application-reference").Element("document-id").Element("doc-number"),
appDate = (from p in theCases.Descendants("document-id")
where (string)theCases.Element("bibliographic-data").Element("application-reference").Element("document-id").Attribute("document-id-type") == "epodoc"
select (p.Element("date").Value)),

  };

        foreach (var case_ in myCase)
        {
            System.Windows.Forms.MessageBox.Show(case_.appDate.ToString());
        }

我正在使用的XML如下:

<exchange-document system="ops.epo.org" family-id="8487663" country="EP" doc- number="0173444" kind="A1">
<bibliographic-data>
<publication-reference>
  <document-id document-id-type="docdb">
    <country>EP</country>
    <doc-number>0173444</doc-number>
    <kind>A1</kind>
    <date>19860305</date>
  </document-id>
  <document-id document-id-type="epodoc">
    <doc-number>EP0173444</doc-number>
    <date>19860305</date>
  </document-id>
</publication-reference>
<classification-ipc>
  <text>B27M3/06</text>
</classification-ipc>
<classifications-ipcr>
  <classification-ipcr sequence="1">
    <text>B27M   1/    04            A I                    </text>
  </classification-ipcr>
  <classification-ipcr sequence="2">
    <text>B27M   3/    04            A I                    </text>
  </classification-ipcr>
  <classification-ipcr sequence="3">
    <text>B44C   3/    12            A I                    </text>
  </classification-ipcr>
</classifications-ipcr>
<patent-classifications>
  <patent-classification sequence="1">
    <classification-scheme office="" scheme="CPC" />
    <section>B</section>
    <class>44</class>
    <subclass>C</subclass>
    <main-group>3</main-group>
    <subgroup>12</subgroup>
    <classification-value>I</classification-value>
  </patent-classification>
  <patent-classification sequence="2">
    <classification-scheme office="" scheme="CPC" />
    <section>B</section>
    <class>27</class>
    <subclass>M</subclass>
    <main-group>1</main-group>
    <subgroup>04</subgroup>
    <classification-value>I</classification-value>
  </patent-classification>
  <patent-classification sequence="3">
    <classification-scheme office="" scheme="CPC" />
    <section>B</section>
    <class>27</class>
    <subclass>M</subclass>
    <main-group>3</main-group>
    <subgroup>04</subgroup>
    <classification-value>I</classification-value>
  </patent-classification>
</patent-classifications>
<application-reference doc-id="16601238">
  <document-id document-id-type="docdb">
    <country>EP</country>
    <doc-number>85305178</doc-number>
    <kind>A</kind>
  </document-id>
  <document-id document-id-type="epodoc">
    <doc-number>EP19850305178</doc-number>
    <date>19850719</date>
  </document-id>
  <document-id document-id-type="original">
    <doc-number>85305178</doc-number>
  </document-id>
</application-reference>

我似乎无法得到我的查询但是要带回任何结果。

2 个答案:

答案 0 :(得分:1)

appDate应与

对应
appDate = (from p in theCases.Descendants("document-id")
where (string)p.Attribute("document-id-type") == "epodoc"
select (p.Element("date").Value)).FirstOrDefault()

答案 1 :(得分:1)

根据Anirudh的帖子:

appDate = (from p in theCases.Descendants("application-reference").Descendants("document-  id") 
where (string)p.Attribute("document-id-type") == "epodoc" 
select (p.Element("date").Value)).FirstOrDefault()

Anirudh对帖子的更改是添加了另一个函数调用:

.Descendants("document-id") 

这是必需的,因为XML中有多个“document-id”元素。