Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从XmlDocument中提取XML元素_C#_Xml_Soap_Xmldocument_Envelope - Fatal编程技术网

C# 从XmlDocument中提取XML元素

C# 从XmlDocument中提取XML元素,c#,xml,soap,xmldocument,envelope,C#,Xml,Soap,Xmldocument,Envelope,我对XML还是个新手,在这个项目上遇到了麻烦。我需要从C#XmlDocument中提取一个特定的xml元素。在下面的示例中,我想从RATING标签中取出ns:AMOUNT元素(结果应该是193.13)。您如何正确地执行此操作?非常感谢 <?xml version="1.0" encoding="UTF-8" ?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

我对XML还是个新手,在这个项目上遇到了麻烦。我需要从C#XmlDocument中提取一个特定的xml元素。在下面的示例中,我想从RATING标签中取出ns:AMOUNT元素(结果应该是193.13)。您如何正确地执行此操作?非常感谢

<?xml version="1.0" encoding="UTF-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:getResp xmlns:ns="http://example">
            <ns:jobReturn>
                <ns:ITEM>
                    <ns:AMOUNT>24.7</ns:AMOUNT>
                </ns:ITEM>
                <ns:RATING>
                    <ns:RefNum>1234567890</ns:RefNum>
                    <ns:AMOUNT>193.13</ns:AMOUNT>
                </ns:RATING>
            </ns:jobReturn>
        </ns:getResp>
    </soapenv:Body>
</soapenv:Envelope>

24.7
1234567890
193.13

我认为您的问题在于需要XmlNamespaceManager来处理ns部分。所以

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"your.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
ns.AddNamespace("ns", "http://example");
XmlNode node = xmlDoc.SelectSingleNode("//ns:RATING/ns:AMOUNT", ns);
var result = node.InnerText;