【问题标题】:Issue with get xpath获取 xpath 的问题
【发布时间】:2017-10-30 07:16:22
【问题描述】:

我正在尝试从 xml 中读取一些数据。下面是我的xml结构。

 <Configuration> 
<node1></node1> 
<unity
 xmlns=xmlns="http://schemas.microsoft.com/practices/2010/unity">
 <namesapce name="somename"/> . . . 
</unity> 
</Configuration>

我尝试使用下面的 xpath 读取节点“unity”

 var nodelist=doc.SelectNodes("/configuration/unity[@xmlns='http://schemas.microsoft.com/practices/2010/unity']");

但它返回 null。我的代码有什么问题?

【问题讨论】:

标签: c# xml xpath


【解决方案1】:

试试这个:(忽略命名空间)

    string xmlString = "<Configuration><node1></node1><unity xmlns=\"http://schemas.microsoft.com/practices/2010/unity\"><namesapce name=\"somename\"/></unity></Configuration>";
    XmlDocument xd = new XmlDocument();
    xd.LoadXml(xmlString);
    var nodes = xd.SelectNodes("//*[local-name()='unity']");

    Console.WriteLine(nodes.Count);

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多