问题
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.
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.)
This is the 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.
回答1:
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?
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
.
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.
来源:https://stackoverflow.com/questions/38125692/c-sharp-linq-xml-query