[英]How to read a node in an xml in this format
I have an getting an xml response on this format 我收到这种格式的xml响应
<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n
<PlatformResponse xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://platform.intuit.com/api/v1\">\r\n
<ErrorMessage>OAuth Token rejected</ErrorMessage>\r\n
<ErrorCode>270</ErrorCode>\r\n
<ServerTime>2012-06-19T00:01:31.5150146Z</ServerTime>\r\n
</PlatformResponse>
I need to grab the value in the <ErrorCode>
node, for that I did the following but it is not getting any values.. 我需要获取<ErrorCode>
节点中的值,为此,我执行了以下操作,但未获取任何值。
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlResponse);
XmlNodeList xnList = xml.SelectNodes("PlatformResponse");
foreach (XmlNode xn in xnList)
{
result.Message = xn["ErrorCode"].InnerText;
}
Any help would be much appreciated. 任何帮助将非常感激。
There seems to be some dirt in your PlatformResponse node that's giving problems, ( xmlns:xsd= etc... ) 您的PlatformResponse节点中似乎存在一些污垢,这会带来问题,(xmlns:xsd =等)
Using this xml 使用这个xml
String sXml = @"<?xml version='1.0' encoding='utf-8'?>
<PlatformResponse >
<ErrorMessage>OAuth Token rejected</ErrorMessage>
<ErrorCode>270</ErrorCode>
<ServerTime>2012-06-19T00:01:31.5150146Z</ServerTime>
</PlatformResponse>";
And select like 然后选择喜欢
XmlNodeList xnList = xml.SelectNodes("/PlatformResponse");
Your code works fine. 您的代码工作正常。
I just tested the code and it does work fine: 我刚刚测试了代码,它确实可以正常工作:
XmlDocument xml = new XmlDocument();
XmlTextReader reader = new XmlTextReader("Path_to_your_xml");
xml.Load(reader);
XmlNodeList xnList = xml.SelectNodes("PlatformResponse");
foreach (XmlNode xn in xnList)
{
MessageBox.Show(xn["ErrorCode"].InnerText);
}
For this, since the attribute is on the main document element itself, you can simply do 为此,由于属性位于主文档元素本身上,因此您只需
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlText);
result.Message = xml.DocumentElement["ErrorCode"].InnerText
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.