<?xml version="1.0" encoding="utf-8"?>
<GN>
  <name ID="01" TEXT="角色管理" QXVIS="True" APPNAME="X1" QX="ABC" VisB="True" LURL="WebAccInfo.aspx" FSTYLE="" />
  <name ID="02" TEXT="我的装备" QXVIS="True" APPNAME="X2" QX="ABC" VisB="True" LURL="Webuserck.aspx" FSTYLE="color:#FF0000;font-weight: bold;" />
</GN>
像这种类型的XML怎么读取id,Text等里面的属性值?

解决方案 »

  1.   

    xmlName.Attributes.GetNamedItem("id").InnerText;
      

  2.   


    //用DOM
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.Load(Server.MapPath("xml.xml"));  
    System.Xml.XmlNodeList list = doc.GetElementsByTagName("name");
    foreach (System.Xml.XmlElement element in list)
    {
        Response.Write(string.Format("ID:{0} __ Name:{1}<br />", element.GetAttribute("ID").ToString(), element.GetAttribute("TEXT").ToString()));
        
    }
      

  3.   


    //使用XmlTextReader
    //效率比DOM高XmlTextReader reader = new XmlTextReader(Server.MapPath("xml.xml"));
    while (reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element)
        {
            if (reader.Name == "name")  //如果是<name>
            {
                while (reader.MoveToNextAttribute())
                {
                    if (reader.Name == "ID")    //ID属性
                    {
                        Response.Write(string.Format("ID: {0}", reader.Value));
                    }
                    if (reader.Name == "TEXT")  //TEXT属性
                    {
                        Response.Write(string.Format("Text: {0}", reader.Value));
                    }
                }
                
                Response.Write("<br />");
            }
        }
    }
      

  4.   

    一般操作结点树的话用xmlnode
    假如要取结点属性的话 用xmlelement比较好..
      

  5.   

    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.Load(Server.MapPath("xml.xml"));  
    foreach (XmlNode tmpNode in doc.SelectSingleNode("GN").ChildNodes)
      {
        tmpNode.Attributes["id"].Value;
        tmpNode.Attributes["name"].Value
      } //循环读取