2016-06-30 6 views
0

私はLINQで作業するための新しいです。何らかの理由で、私はそれを消化するのに非常に苦労しています。私が持っているXML文書はかなりシンプルなようですが、私を助けているページは見つけられません。C#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; 

私はまだこれを理解しようとしているので、私は必要なすべての情報を提供していると確信しています。私が迷っていることを教えてください。

答えて

1

Action要素のうち、Attribute属性の値がurn:oasis:names:tc:xacml:1.0:action:action-idのものを探しています。それは正しいとは思いませんか?

要素に属し、AttributeId属性の値がurn:oasis:names:tc:xacml:1.0:action:action-idAttributeValue要素です。

ルート要素に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を参照してください。

+0

これはすばらしいです!早速のご返事ありがとうございます。それはまさに私が必要とするものです。 –