【发布时间】:2020-05-29 01:51:49
【问题描述】:
我已经阅读并尝试了许多线程 - this answer、this 和 this answer。但是,它们不适用于我,因为我真的没有通常的 xml:
var xmlString = @"<?xml version=""1.0"" encoding=""windows-1251""?>
<GetReply>
<InformOne>87</InformOne>
<InfoReply>
<![CDATA[<?xml version='1.0' encoding='UTF-8'?>
<S:Container xmlns:S=""http://schemas.xmlsoap.org/soap/envelope/"">
<S:Body>
<ns2:getReference31IPOResponse xmlns:ns2 = ""http://service.one.com/"" >
<return>
<reference31_1IPO xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:nil=""true""/>
<reference31_2IPO xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:nil=""true""/>
<amount>0</amount>
<codeTypeObject>0</codeTypeObject>
<returnCode>4</returnCode>
<errorCode>0</errorCode>
<errorMessage>Something was wrong</errorMessage>
<title>Foo Data</title>
</return>
</ns2:getReference31IPOResponse>
</S:Body>
</S:Container>]]>
</InfoReply>
</GetReply>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
var errorMessage = xmlDoc.SelectSingleNode("/GetReply/InformOne/InfoReply/CDATA/S:Container/S:Body/ns2:getReference31IPOResponse/return/errorMessage");
但是,我看到以下错误:
'表达式必须计算为节点集。'
另外,我什至尝试获取InfoReply,但是错误是一样的:
var errorMessage = xmlDoc.SelectSingleNode("/GetReply/InformOne/InfoReply/");
我想要阅读errorMessage 节点中的文本?
你能告诉我,我做错了什么吗? 任何帮助将不胜感激。
看起来<![CDATA[<?xml version='1.0' encoding='UTF-8'?> 中断读取其余节点。
【问题讨论】:
-
那么,您想要来自
<errorMessage>Something was wrong</errorMessage>的值吗? -
@PavelAnikhouski 你是绝对正确的
-
为什么不用
Xml.Linq呢? -
@PavelAnikhouski 好的,让我们尝试使用
Xml.linq。
标签: c# xml xmldocument