C# 读取XML文件某个节点的值

wanghousheng 2009-05-15 09:03:30
xml文件格式及内容如下
<?xml version="1.0" encoding="utf-8" ?>
- <response>
- <TAG>
<tagid>1234</tagid>
<mac>00:00:00:00:04:D2</mac>
<type>t201</type>
<createdon>2009-05-13 16:08:47+0800</createdon>
<serialnumber />
<swversion />
<hwversion />
<wlanfirmware />
<cmdsetversion>0</cmdsetversion>
<hellocounter>0</hellocounter>
<hellotimestamp>2009-05-13 16:08:47+0800</hellotimestamp>
<confirmcounter>0</confirmcounter>
<configid>0</configid>
<tagmenuid>0</tagmenuid>
</TAG>
- <TAG>
<tagid>105461601172</tagid>
<mac>00:18:8E:00:53:94</mac>
<type>t301d</type>
<createdon>2009-05-12 14:29:41+0800</createdon>
<serialnumber>301A-0801-022395</serialnumber>
<swversion>2.1.2</swversion>
<hwversion />
<wlanfirmware />
<cmdsetversion>2</cmdsetversion>
<ip>192.168.0.200</ip>
<apmac>00:22:6B:6A:66:06</apmac>
<cmdresult>0</cmdresult>
<hellocounter>1225</hellocounter>
<hellotimestamp>2009-05-15 08:57:22+0800</hellotimestamp>
<confirmcounter>6</confirmcounter>
<confirmtimestamp>2009-05-14 11:44:41+0800</confirmtimestamp>
<configid>32</configid>
<tagmenuid>0</tagmenuid>
<posx>667</posx>
<posy>542</posy>
<posmodelid>25</posmodelid>
<posmapid>0</posmapid>
</TAG>
</response>
现在要根据tagid 是105461601172 ,得到该tagid的mac、type和ip,请问如何得到?用c#,
...全文
1875 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
草根豆 2010-11-30
  • 打赏
  • 举报
回复
取不到节点的值 ,郁闷 .
junweishiwo 2009-05-15
  • 打赏
  • 举报
回复
XmlNode GetXMLNode(string strPath(XML文件全路径), string StatName(NodeList名), object XmlNodeValue(node名))
{
XmlNode pXmlNode = null;
try
{
XmlDocument pXmlDoc = new XmlDocument();
pXmlDoc.Load(strPath);
XmlElement xe = pXmlDoc.DocumentElement;
XmlNode xmlnode =
xe.SelectSingleNode("descendant::" + StatName + "[@NAME='" + XmlNodeValue + "']");
pXmlNode = xmlnode;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return pXmlNode;
}
zhubo_1117 2009-05-15
  • 打赏
  • 举报
回复

XmlDocument xml = new XmlDocument();
//如果是字符串
// String str = "<><>";
// xml.LoadXml(str);
//如果是文件
xml.Load(Server.MapPath("test.xml"));
XmlNodeList node = xml.SelectNodes("//TAG/tagid");
XmlNode resultNode = null;
foreach(XmlNode item in node)
{
if (item.InnerText == "105461601172 ")
{
resultNode = item;
break;
}
}

这样可以的!
xutao888 2009-05-15
  • 打赏
  • 举报
回复
上面的太复杂了
你可以把XML先DataSet.ReadXML到DataSet里面

然后用表的形式读取,这样XML即使有小的改动也不会收到影响
deyter 2009-05-15
  • 打赏
  • 举报
回复
读取节点值用GetNodeValue不就可以了啊
enihs 2009-05-15
  • 打赏
  • 举报
回复
            
XmlDocument doc = new XmlDocument();
doc.Load(@"文件位置");

XmlNode node = doc.SelectSingleNode("response/TAG[tagid=105461601172]");//根据xpath,这里我把你数字后面的空格去掉了
string mac = node.ChildNodes[1].InnerText;//根据排列规律(tag下面的第二个节点)
string type = node.ChildNodes[2].InnerText;
string ip = node.ChildNodes[9].InnerText;


根据xml文档的dom结构和xpath做的,还可以用正则表达式方式
ljhcy99 2009-05-15
  • 打赏
  • 举报
回复
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\1.xml");
XmlNodeList list = doc.DocumentElement.GetElementByTagName("TAG");

foreach(Xmlnode node in list)
{
if(node.ChildNodes[0].InnerText = "105461601172")
{
Console.WriteLine(node.ChildNodes[1].InnerText); //mac
Console.WriteLine(node.ChildNodes[2].InnerText); //type
Console.WriteLine(node.ChildNodes[9].InnerText); //ip
}
}
whyabc 2009-05-15
  • 打赏
  • 举报
回复
up
wanghousheng 2009-05-15
  • 打赏
  • 举报
回复
具体说一下呗,呵呵,你这样说我也不明白么,谢谢你啊,麻烦你帮忙写个方法。谢谢
wts_net 2009-05-15
  • 打赏
  • 举报
回复
用xml提供的查找节点的方法

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧