获取XML单个节点的值!
XML文件如下:
<return msgid="123">
< arguments >
<string id="devicecode">ZHAOYINGBI</string>
<string id="command">OPENDEV</string>
<integer id="result">0</integer>
</arguments>
</return>
1. 获取<return msgid="123"> 中的msgid的值。
2. 获取<integer id="result">0</integer> 的值(即 0)。
请高手指教!万分感激!!!
[解决办法]
_xmlDoc.Load(xmlPath); //载入Xml文档
XmlNode firstNode = _xmlDoc.SelectSingleNode("return");
if (firstNode != null)
{
XmlAttributeCollection xmlAttributes = node.Attributes;
msgid = xmlAttributes["msgid"].Value;
XmlNodeList nodes = firstNode.SelectSingleNode("arguments").SelectNodes("string");
if (nodes != null)
{
foreach (XmlNode node in nodes)
{
XmlAttributeCollection xmlAttributes = node.Attributes;
if (xmlAttributes["id"].Value == "result")
{
itemText = xmlAttributes[itemValueName].InnerText;
break;
}
}
}
}
好像整复杂了。
[解决办法]
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Server.MapPath(@"c:\test.xml")); Console.WriteLine(xmlDoc.DocumentElement.Attributes["msgid"].Value); Console.WriteLine(xmlDoc.SelectSingleNode(@"//integer[@id='result']").InnerText);