首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

获取XML单个节点的值!该如何解决

2012-10-30 
获取XML单个节点的值!XML文件如下:return msgid123 arguments string iddevicecodeZHAOYINGBI

获取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;
}
}
}
}
好像整复杂了。
[解决办法]

C# code
        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); 

热点排行