【问题标题】:Get element from xml node C#从 xml 节点 C# 获取元素
【发布时间】:2016-05-05 12:44:37
【问题描述】:

我有 xml,我想获取 node 的值。我的 XML 外观:

<?xml version="1.0" encoding="UTF-8"?>
<jdf:root xmlns:jdf="xxxxxxxx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<jdf:header>

    <jdf:locale-code>xx</jdf:locale-code>
    <jdf:country-code>xx</jdf:country-code>
</jdf:header>
<app:data xmlns:app="xxxxxx">
    <app:EventOut xmlns:ns2="xxxxxxx">
        <app:eventId>xxx</app:eventId>
        <app:distributorId>xxx</app:distributorId>
        <app:distributionNetworkId>xxx</app:distributionNetworkId>
        <app:typology>xxx</app:typology>
        <app:targets>
            <app:target>
                ......
            </app:target>
            <app:target>
                .....
            </app:target>
        </app:targets>
        <app:object>
            <ns2:internalEventObject>
                <ns2:id>!!!!!!!!</ns2:id>
                <ns2:lang1>xxx</ns2:lang1>
            </ns2:internalEventObject>
        </app:object>
        ...
    </app:EventOut>
</app:data>

我只是试试:

    XmlDocument xml = new XmlDocument();
    xml.LoadXml(eventOutXml);

    var nsmgr = new XmlNamespaceManager(xml.NameTable);
    nsmgr.AddNamespace("ns2", "http://www.w3.org/1999/XSL/Transform");

    XmlNode anode = xml.SelectSingleNode("//ns2:id", nsmgr);

但它不起作用。

在我的 XML 中,我有几个命名空间:jdf、app、ns2。也许我必须添加所有这些?

【问题讨论】:

  • “它不工作。”怎么样?
  • 我可能错了,但我认为SelectSingleNode 中不需要ns2: 命名空间前缀。
  • 你能确保 ns2 命名空间值与你在 c# 代码中使用的相同吗?
  • @krolik1991 你确定ns2 在声明为这样的实际XML 中:xmlns:ns2="http://www.w3.org/1999/XSL/Transform" 吗?

标签: c# xml nodes


【解决方案1】:

您的 xml 缺少结束标记。而且你在代码中添加的命名空间在xml中是不同的。我对 xml 进行了这两项更改,并且能够使其正常工作。

更新的 xml:

<?xml version="1.0" encoding="UTF-8"?>
<jdf:root xmlns:jdf="xxxxxxxx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<jdf:header>
    <jdf:locale-code>xx</jdf:locale-code>
    <jdf:country-code>xx</jdf:country-code>
</jdf:header>
<app:data xmlns:app="xxxxxx">
    <app:EventOut xmlns:ns2="http://www.w3.org/1999/XSL/Transform">
        <app:eventId>xxx</app:eventId>
        <app:distributorId>xxx</app:distributorId>
        <app:distributionNetworkId>xxx</app:distributionNetworkId>
        <app:typology>xxx</app:typology>
        <app:targets>
            <app:target>
                ......
            </app:target>
            <app:target>
                .....
            </app:target>
        </app:targets>
        <app:object>
            <ns2:internalEventObject>
                <ns2:id>!!!!!!!!</ns2:id>
                <ns2:lang1>xxx</ns2:lang1>
            </ns2:internalEventObject>
        </app:object>
        ...
    </app:EventOut>
</app:data>
</jdf:root>

在您的代码之后,只需使用它来获取值。

var value = anode.InnerText; //!!!!!!!!

让我知道这是否有效!

【讨论】:

  • 我注意到缺少的标签,我只是假设 OP 没有意外复制整个内容。但是,嘿,这可能是问题的全部根源。
  • 是的,它有效!谢谢!我在 c# 代码中添加了错误的命名空间。 xml 没有结束标记,因为它不是 xml 代码的全部。
【解决方案2】:

写下该节点的整个路径。

XmlNode anode = xml.SelectSingleNode("/ns2:internalEventObjects/ns2:id", nsmgr);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2021-12-18
    • 2012-12-12
    • 2016-02-26
    • 1970-01-01
    相关资源
    最近更新 更多