C# LINQ XML 查询
C# LINQ XML Query
我是 LINQ 的新手。不管出于什么原因,我都很难消化它。我拥有的 XML 文档似乎非常简单,但我没有找到任何对我有帮助的页面。
我需要从每个字段中提取 AttributeValue 字段。 (不是针对每个,而是谨慎地存储在每个 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>
到目前为止我尝试过的方法没有用:
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;
由于我仍在努力解决这个问题,我确信我提供了所有需要的信息。让我知道我错过了什么。
您正在查找具有 Attribute
属性且值为 urn:oasis:names:tc:xacml:1.0:action:action-id
的 Action
元素。听起来不太对吧?
您真正想要查找的是 AttributeValue
元素,该元素属于具有 AttributeId
属性且值为 urn:oasis:names:tc:xacml:1.0:action:action-id
.[=20= 的 Attribute
元素]
您还需要考虑命名空间,它在根元素中指定为 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();
有关工作演示,请参阅 this fiddle。
我是 LINQ 的新手。不管出于什么原因,我都很难消化它。我拥有的 XML 文档似乎非常简单,但我没有找到任何对我有帮助的页面。
我需要从每个字段中提取 AttributeValue 字段。 (不是针对每个,而是谨慎地存储在每个 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>
到目前为止我尝试过的方法没有用:
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;
由于我仍在努力解决这个问题,我确信我提供了所有需要的信息。让我知道我错过了什么。
您正在查找具有 Attribute
属性且值为 urn:oasis:names:tc:xacml:1.0:action:action-id
的 Action
元素。听起来不太对吧?
您真正想要查找的是 AttributeValue
元素,该元素属于具有 AttributeId
属性且值为 urn:oasis:names:tc:xacml:1.0:action:action-id
.[=20= 的 Attribute
元素]
您还需要考虑命名空间,它在根元素中指定为 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();
有关工作演示,请参阅 this fiddle。