c# - linq to xml 中标记的内容

标签 c# xml linq

我正在尝试使用 linq to xml 获取标签的内容。我似乎无法让它工作。我们将不胜感激。

<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>

我的代码;

       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;

最佳答案

你的 <BTMACRequestResponse>元素有一个默认命名空间,因此在匹配第一个元素时必须考虑到它:

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;

关于c# - linq to xml 中标记的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6455086/

相关文章:

c# - 如何使用字符串中的 XML 标记?

c# - 在 Linq 中使用 OrderBy

c# - 如何获取最近编辑的文件列表?

c# - 一些基本的 UML 问题

xml - 如何在xquery中的 "if statements"语句中使用 "let"

java - 如何在 Tomcat 的 server.xml 中保护 LDAP 领域定义

c# - 将时差与固定周期进行比较

c# - 我怎样才能在 C# 中获得这个正则表达式?

c# - 如何在 LINQ to XML 中选择具有命名空间的 XML 节点

c# - 使用正则表达式列表查找匹配的目录