[英]C# LINQ XML Query
我是使用LINQ的新手。 無論出於什么原因,我都很難消化它。 我所擁有的XML文檔似乎非常簡單,但是找不到任何有幫助的頁面。
我需要從每個提取AttributeValue字段。 (而不是每個,而是謹慎地存儲在每個AttributeId的專用var中。)
這是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
元素,該元素屬於Attribute
,其AttributeId
屬性值為urn:oasis:names:tc:xacml:1.0:action:action-id
。
您還需要考慮名稱空間,該名稱空間在根元素中指定為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();
請參閱此小提琴以獲得有效的演示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.