【问题标题】:How to get value of an XML node?如何获取 XML 节点的值?
【发布时间】:2018-08-22 21:36:53
【问题描述】:

在我的一生中,我无法从这个 XML 文档中提取 SourcePartyName

<ns0:Visit xmlns:ns0="http://Co.Burgers.Ues">
<ns0:SourcePartyName>NDHARY</ns0:SourcePartyName>
</ns0:Visit>

使用 Scott 的solution,我已经能够提取命名空间信息;然而,经过数十次与XDocument / XElement 的混搭尝试后,我无法获得所需的NDHARY 值。

尝试包括:

xdoc.Descendants(ns + "SourcePartyName").FirstOrDefault()?.Value;

xdoc.Element(ns + "SourcePartyName").Value;

如何从 XDocument 中获取节点的值?

【问题讨论】:

  • 尝试以下操作:xdoc.Descendants().Where(x => x.Name.LocalName == "SourcePartyName").Select(x => (string)x).FirstOrDefault()跨度>

标签: c# .net xml linq-to-xml


【解决方案1】:

使用XDocument 时,您必须通过其Root 属性。

String xml = @"
    <ns0:Visit xmlns:ns0=""http://Co.Burgers.Ues"">
        <ns0:SourcePartyName>NDHARY</ns0:SourcePartyName>
    </ns0:Visit>
    ";
XDocument xdoc = XDocument.Parse(xml);
XNamespace ns = "http://Co.Burgers.Ues";
String sourcePartyName = (String)xdoc.Root.Element(ns + "SourcePartyName");

【讨论】:

    猜你喜欢
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2013-08-12
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    相关资源
    最近更新 更多