C#LINQ XML查询 [英] C# LINQ XML Query
问题描述
我是刚接触LINQ的新手.无论出于什么原因,我都很难消化它.我拥有的XML文档似乎非常简单,但是找不到任何对我有帮助的页面.
I am brand new to working with LINQ. For whatever reason, I am having a very hard time digesting it. The XML document I have seems to be pretty simple, but I am not finding any pages that are helping me.
我需要从每个提取AttributeValue字段. (不是每个属性的,而是谨慎地存储在每个AttributeId的专用var中.)
I need to extract the AttributeValue field from each. (Not as a for each, but discreetly and store in a dedicated var for each AttributeId.)
这是XML:
<?xml version="1.0" encoding="UTF-8"?>
<Request xmlns="urn:oasis:names:tc:xacml:2.0:context:schema:os">
<Subject SubjectCategory="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject">
<Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:subject:role-id" DataType="http://www.w3.org/2001/XMLSchema#string" Issuer="requestor">
<AttributeValue>CISCO:UC:UCMPolicy</AttributeValue>
</Attribute>
<Attribute AttributeId="urn:Cisco:uc:1.0:callingnumber" DataType="http://www.w3.org/2001/XMLSchema#string">
<AttributeValue>10000</AttributeValue>
</Attribute>
<Attribute AttributeId="urn:Cisco:uc:1.0:callednumber" DataType="http://www.w3.org/2001/XMLSchema#string">
<AttributeValue>94146172510</AttributeValue>
</Attribute>
<Attribute AttributeId="urn:Cisco:uc:1.0:transformedcgpn" DataType="http://www.w3.org/2001/XMLSchema#string">
<AttributeValue>10000</AttributeValue>
</Attribute>
<Attribute AttributeId="urn:Cisco:uc:1.0:transformedcdpn" DataType="http://www.w3.org/2001/XMLSchema#string">
<AttributeValue>888884146172510</AttributeValue>
</Attribute>
</Subject>
<Resource>
<Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:resource:resource-id" DataType="http://www.w3.org/2001/XMLSchema#anyURI">
<AttributeValue>CISCO:UC:VoiceOrVideoCall</AttributeValue>
</Attribute>
</Resource>
<Action>
<Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:action:action-id" DataType="http://www.w3.org/2001/XMLSchema#anyURI">
<AttributeValue>any</AttributeValue>
</Attribute>
</Action>
<Environment>
<Attribute AttributeId="urn:Cisco:uc:1.0:triggerpointtype" DataType="http://www.w3.org/2001/XMLSchema#string">
<AttributeValue>translationpattern</AttributeValue>
</Attribute>
</Environment>
</Request>
到目前为止,我设法尝试的方法不起作用:
What I have managed to try so far is not working:
var query = from t in root.Descendants("Action") where (string)t.Attribute("Attribute") == "urn:oasis:names:tc:xacml:1.0:action:action-id" select t;
由于我仍在设法弄清楚这一点,因此我确定我会提供所需的所有信息.让我知道我在想什么.
As I am still trying to figure this out, I am sure I am giving all the info needed. Let me know what I am missing.
推荐答案
您正在寻找一个具有Attribute
属性且值为urn:oasis:names:tc:xacml:1.0:action:action-id
的Action
元素.听起来不对,是吗?
You're looking for an Action
element that has a Attribute
attribute with the value urn:oasis:names:tc:xacml:1.0:action:action-id
. That doesn't sound right, does it?
您实际要查找的是一个AttributeValue
元素,该元素属于具有AttributeId
属性且值为urn:oasis:names:tc:xacml:1.0:action:action-id
的Attribute
元素.
What you actually want to find is an AttributeValue
element that belongs to an Attribute
element with the AttributeId
attribute with the value urn:oasis:names:tc:xacml:1.0:action:action-id
.
您还需要考虑名称空间,该名称空间在根元素中指定为urn:oasis:names:tc:xacml:2.0:context:schema:os
.
You also need to take the namespace into account, which is specified in the root element as urn:oasis:names:tc:xacml:2.0:context:schema:os
.
XNamespace ns = "urn:oasis:names:tc:xacml:2.0:context:schema:os";
var id = "urn:oasis:names:tc:xacml:1.0:action:action-id";
var value = (string) doc.Descendants(ns + "Attribute")
.Where(x => (string)x.Attribute("AttributeId") == id)
.Elements(ns + "AttributeValue")
.Single();
有关工作示例,请参见此小提琴.
See this fiddle for a working demo.
这篇关于C#LINQ XML查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!