如何使用 LINQ 查询 XML 文档?
假设我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以假设每个子元素仅出现一次,则可以尝试此方法:
如果要在解析 XML 的方法之外使用此数据,您可能还需要考虑使用具体类型而不是匿名类型。
If you can assume that each subelement occurs only once you can try this approach:
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.