Content of a tag in linq to xml
Im trying to get the content of a tag using linq to xml. I cant seem to get it to work. Any help would be apreciated.
<BTMACRequestResponse xmlns="http://wholesale.fluidata.co.uk/bt">
<BTMACRequestResult xmlns:a="http://wholesale.fluidata.co.uk/bt/BTMacResponse" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:BTRequestID>0</a:BTRequestID>
<a:dateadded/>
<a:dsl>01491410786</a:dsl>
<a:expiryDate i:nil="true"/>
<a:mac i:nil="true"/>
<a:response xmlns:b="http://schemas.datacontract.org/2004/07/DataLayer">
<b:code>INVALIDACCOUNT</b:code>
<b:message>This account does either not belong to you or is not a valid BT ECO PLUS account.</b:message>
<b:severity i:nil="true"/>
</a:response>
<a:serviceID>6sdfs</a:serviceID>
<a:status i:nil="true"/>
</BTMACRequestResult>
</BTMACRequestResponse>
My Code;
XNamespace a = @"http://wholesale.fluidata.co.uk/bt/BTMacResponse";
XNamespace b = @"http://schemas.datacontract.org/2004/07/DataLayer";
output = (from s in PostToFluidataBTWebservice.MacRequest(number, serviceID).
Descendants("BTMACRequestResponse")
开发者_运维知识库 .Elements("BTMACRequestResult")
.Elements(a + "response")
.Elements(a + "message")
select s).First().Value;
Your <BTMACRequestResponse>
element has a default namespace, so you have to take it into account when matching the first elements:
XNamespace ns = "http://wholesale.fluidata.co.uk/bt";
XNamespace a = "http://wholesale.fluidata.co.uk/bt/BTMacResponse";
XNamespace b = "http://schemas.datacontract.org/2004/07/DataLayer";
var output = PostToFluidataBTWebservice.MacRequest(number, serviceID)
.Descendants(ns + "BTMACRequestResponse")
.Elements(ns + "BTMACRequestResult")
.Elements(a + "response")
.Elements(b + "message")
.First().Value;
This works for me:
XDocument doc = XDocument.Load("test.xml");
XNamespace a = @"http://wholesale.fluidata.co.uk/bt/BTMacResponse";
XNamespace b = @"http://schemas.datacontract.org/2004/07/DataLayer";
XNamespace g = "http://wholesale.fluidata.co.uk/bt";
var output = (from s in doc.Descendants(g+ "BTMACRequestResponse")
.Elements(g+"BTMACRequestResult")
.Elements(a + "response")
.Elements(b + "message")
select s).First().Value;
try correcting these line like so:
.Elements(a + ":response")
.Elements(b + ":message")
精彩评论