如何使用 LINQ 查询 XML 文档?

发布于 2024-10-16 03:03:50 字数 2102 浏览 3 评论 0原文

假设我有以下 XML 文档:

<response>
   <businessEntity>
      <ABN>
         <identifierValue></identifierValue> 
         <isCurrentIndicator></isCurrentIndicator> 
         <replacedIdentifierValue xsi:nil="true" /> 
         <replacedFrom></replacedFrom> 
      </ABN>
      <entityStatus>
         <entityStatusCode> </entityStatusCode> 
         <effectiveFrom></effectiveFrom> 
         <effectiveTo></effectiveTo> 
      </entityStatus>
      <entityType>
         <entityTypeCode> </entityTypeCode> 
         <entityDescription> </entityDescription> 
      </entityType>
      <goodsAndServicesTax>
          <effectiveFrom></effectiveFrom> 
          <effectiveTo></effectiveTo> 
      </goodsAndServicesTax>
      <legalName>
          <givenName> </givenName> 
          <otherGivenName /> 
          <familyName> </familyName> 
          <effectiveFrom></effectiveFrom> 
          <effectiveTo></effectiveTo> 
      </legalName>
      <mainBusinessPhysicalAddress>
          <stateCode> </stateCode> 
          <postcode></postcode> 
          <effectiveFrom></effectiveFrom> 
          <effectiveTo></effectiveTo> 
      </mainBusinessPhysicalAddress>
   </businessEntity>
</response>

例如,如果我想获取给定名称和家庭名称,我可以这样做:

var businesses = doc.Descendants(ns + "businessEntity")
             .Elements(ns + "legalName")
             .Select(node => new
             {
                 effectiveFrom = node.Element(ns + "effectiveFrom").Value,
                 givenName = node.Element(ns + "givenName").Value,
                 familyName = node.Element(ns + "familyName").Value,
             }).ToList();

然后我可以访问并填充表格,但我不确定如何查询整个文档并获取所有内容我想要的节点,例如 ABN 和实体类型。我是否需要创建一个列表并将每个同级的部分代码复制到 legalName?

提前致谢。

Lets say I have the following XML document:

<response>
   <businessEntity>
      <ABN>
         <identifierValue></identifierValue> 
         <isCurrentIndicator></isCurrentIndicator> 
         <replacedIdentifierValue xsi:nil="true" /> 
         <replacedFrom></replacedFrom> 
      </ABN>
      <entityStatus>
         <entityStatusCode> </entityStatusCode> 
         <effectiveFrom></effectiveFrom> 
         <effectiveTo></effectiveTo> 
      </entityStatus>
      <entityType>
         <entityTypeCode> </entityTypeCode> 
         <entityDescription> </entityDescription> 
      </entityType>
      <goodsAndServicesTax>
          <effectiveFrom></effectiveFrom> 
          <effectiveTo></effectiveTo> 
      </goodsAndServicesTax>
      <legalName>
          <givenName> </givenName> 
          <otherGivenName /> 
          <familyName> </familyName> 
          <effectiveFrom></effectiveFrom> 
          <effectiveTo></effectiveTo> 
      </legalName>
      <mainBusinessPhysicalAddress>
          <stateCode> </stateCode> 
          <postcode></postcode> 
          <effectiveFrom></effectiveFrom> 
          <effectiveTo></effectiveTo> 
      </mainBusinessPhysicalAddress>
   </businessEntity>
</response>

If I want to get the givenName and familyName for example I can do this:

var businesses = doc.Descendants(ns + "businessEntity")
             .Elements(ns + "legalName")
             .Select(node => new
             {
                 effectiveFrom = node.Element(ns + "effectiveFrom").Value,
                 givenName = node.Element(ns + "givenName").Value,
                 familyName = node.Element(ns + "familyName").Value,
             }).ToList();

which I then can access and populate a table with, I am not sure how to query the whole document though and get all the nodes I want, like ABN and entityType for example. Do I need to create a list and replicate part of the code for each sibling to legalName?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再浓的妆也掩不了殇 2024-10-23 03:03:50

如果您可以假设每个子元素仅出现一次,则可以尝试此方法:

var businesses = (
    from node in doc.Descendants(ns + "businessEntity")
    let legalName = node.Element(ns + "legalName")
    let abn = node.Element(ns + "ABN")
    // etc...
    select new
    {
        LegalName = new
        {
            EffectiveFrom = (string)legalName.Element(ns + "effectiveFrom"),
            GivenName = (string)legalName.Element(ns + "givenName"),
            FamilyName = (string)legalName.Element(ns + "familyName"),
        },
        Abn = new
        {
            IdentifierValue = (string)abn.Element(ns + "identifierValue"),
            IsCurrentIndicator = (string)abn.Element(ns + "isCurrentIndicator"),
            ReplacedFrom = (string)abn.Element(ns + "replacedFrom"),
        },
        // etc...
    }).ToList();


Console.WriteLine(businesses[0].LegalName.GivenName);
Console.WriteLine(businesses[0].Abn.IsCurrentIndicator);

如果要在解析 XML 的方法之外使用此数据,您可能还需要考虑使用具体类型而不是匿名类型。

If you can assume that each subelement occurs only once you can try this approach:

var businesses = (
    from node in doc.Descendants(ns + "businessEntity")
    let legalName = node.Element(ns + "legalName")
    let abn = node.Element(ns + "ABN")
    // etc...
    select new
    {
        LegalName = new
        {
            EffectiveFrom = (string)legalName.Element(ns + "effectiveFrom"),
            GivenName = (string)legalName.Element(ns + "givenName"),
            FamilyName = (string)legalName.Element(ns + "familyName"),
        },
        Abn = new
        {
            IdentifierValue = (string)abn.Element(ns + "identifierValue"),
            IsCurrentIndicator = (string)abn.Element(ns + "isCurrentIndicator"),
            ReplacedFrom = (string)abn.Element(ns + "replacedFrom"),
        },
        // etc...
    }).ToList();


Console.WriteLine(businesses[0].LegalName.GivenName);
Console.WriteLine(businesses[0].Abn.IsCurrentIndicator);

You may also want to consider using concrete types instead of anonyous types if this data is going to be used outside the method where you are parsing the XML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文