开发者

Content of a tag in linq to xml

开发者 https://www.devze.com 2023-03-15 03:04 出处:网络
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.

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")
0

精彩评论

暂无评论...
验证码 换一张
取 消