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#,

解决方案 »

  1.   

    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
      }
    }
      

  2.   

                
    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做的,还可以用正则表达式方式
      

  3.   

    http://blog.csdn.net/llwinnner/archive/2009/03/18/4002739.aspx
      

  4.   

    读取节点值用GetNodeValue不就可以了啊
      

  5.   

    上面的太复杂了
    你可以把XML先DataSet.ReadXML到DataSet里面然后用表的形式读取,这样XML即使有小的改动也不会收到影响
      

  6.   


     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;
                    }
                }这样可以的!
      

  7.   

    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;
    }