日期:2014-05-18 浏览次数:20852 次
/// <summary> /// 获取指定节点的值 /// </summary> /// <param name="strFileName">文件路径</param> /// <param name="nodeName">节点名称</param> /// <param name="value">设置后的值</param> /// <param name="nodeDir">指定节点所在的节点目录</param> /// <returns></returns> public static string GetNodeValue(string strFileName, string nodeName, string nodeDir) { string value = null; try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(strFileName); XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点 foreach (XmlNode xn in nodeList) //遍历所有子节点 { XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 if (xe.Name == nodeName) { value = xe.InnerText.Trim(); break; } } } catch (Exception exp) { throw exp; } return value; } [color=#FF0000] GetNodeValue(filePath, "Main", "Result/Flag");[/color]
------解决方案--------------------
string str=@<?xml version=\"1.0\" encoding=\"gb2312\"?>
<Result>
<Main>
<Flag>True</Flag>
<Code>000</Code>
<Message>操作成功</Message>
</Main>
</Result>
";
XmlDocument xml = new XmlDocument();
xml.LoadXml(str);
XmlNodeList xnl = xml.SelectNodes("../Result/Main");
foreach (XmlNode item in xnl)
{
string Flag = item.SelectSingleNode("Flag").InnerText;
string Code= item.SelectSingleNode("Code").InnerText;
string Message = item.SelectSingleNode("Message").InnerText;
}
数据量很多就要用集合保存Flag Code和Message