我有一个XML,我想写两个方法。一个方法得到info里两个节点code、和name的值。
一个方法是得到menus里所有Item里的属性和Value值。
<?xml version="1.0" encoding="UTF-8"?>
<Config>
  <info>
    <code>1</code>
    <name>1.0.0.0</name>
  </info>
  <Category>
    <menus>
      <Item Name="XiTong">系统</Item>
      <Item Name="Bianji">编辑</Item>
      <Item Name="SoSuo">搜索</Item>
      <Item Name="ShiTu">视图</Item>
      <Item Name="Bangzhu">帮助</Item>
    </menus>
  </Category>
</Config>
非常感谢各位!

解决方案 »

  1.   

    参考:http://hi.baidu.com/xiaolg2010/blog/item/edcd7accc3850b0992457ed2.html
      

  2.   

    http://hi.baidu.com/kbsy/blog/item/6ddcb53d52d51acb9f3d629f.html
      

  3.   

    C#处理XML这种格式应该是很方便的吧。
      

  4.   

                    XmlTextReader reader = new XmlTextReader(_xmlPath);
                    string str = "";
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            switch (reader.Name)
                            {
                                case "code":
                                    ht.Add("code", reader.Value);
                                    break;
                                case "name":
                                    ht.Add("name", reader.Value);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }这样的方式取不到值啊!reader.Value
      

  5.   

    能不能有种方法我直接历遍/Config/info 和 /Config/Category/menus分别从这两个节点开始历遍这样寻找会准确些!
      

  6.   


            private void Test()
            {            XElement x = XElement.Load(@"e:\1.xml");
                var infos = from node in x.Descendants("info")
                           select new
                           {
                               code = node.Element("code").Value,
                               name = node.Element("name").Value,
                           };            var menus = from node in x.Element("Category").Element("menus").Descendants("Item")
                           select new
                           {
                               name = node.Attribute("Name").Value,
                               text = node.Value,
                           };
            }如果你的XML已经在内存中,例如是一个字符串,可以这样进行读取XElement x = XElement.Parse("<?xml version=...");
      

  7.   

    7楼前辈!我用的。net2.0 XElement 没有这个类!不好意思!
      

  8.   

    特意冲妹子进来看看
    给你个流氓的写法DataSet ds =new DataSet();
    ds.ReadXml(你的文件或流);DataTable info =ds.Tables["info"];
    DataTable menus=ds.Tables["Item"];
    拼写可能有错误,纯手打 王甜甜同学
      

  9.   

    怎么这么复杂..直接这样就可以啊..其他节点类似..               string path = @"a.xml";
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode("/Config/info/name");
                    if (xn != null)
                        Console.WriteLine(xn.InnerXml);
      

  10.   


    XmlDocument doc=new XmlDocument();
    doc.Load(@"c:\test.xml");string path="/Config/info/code";
    XmlNode node=doc.SelectSingleNode(path);
    string val=node.Value;//code的值path="/Config/info/name";
    node=doc.SelectSingleNode(path);
    val=node.Value;      //name的值path="/Config/Category/menus/Item";
    XmlNodeList nodeList=doc.SelectNodes(path);foreach(XmlNode sub in nodeList)
    {
       string attr=sub.Attributes["Name"].Value ;//Item属性
       val=sub.Value;   //Item值
    }
      

  11.   

    我这样做的成功了!XmlNodeList nodeList = xmldoc.SelectNodes("//Config//info");
                    foreach (XmlNode node in nodeList)
                    {
                        foreach (XmlNode childnode in node.ChildNodes)
                        {
                            string aaa=childnode.Name;
                            string bbb=childnode.InnerText;
                        }
                    }
      

  12.   

    用Xpath获取数据,发现很多人都不会。难得6楼的兄弟还记得 XmlDocument xmldoc = new XmlDocument();
    xmldoc.LoadXml(xmlstr);
    //获取指定节点
    string code = xmldoc.SelectSingleNode("/Config/info/code").InnerText;
    string name = xmldoc.SelectSingleNode("/Config/info/name").InnerText;

    //获取全部Item节点
    XmlNodeList nodes = xmldoc.SelectNodes("/Config/Category/menus/Item");
    List<string[]> list = new List<string[]>(nodes.Count);
    foreach(XmlNode node in nodes)
    {
    string[] strarr = new string[2];
    strarr[0] = node.InnerText;
    strarr[1] = node.Attributes["Name"].Value;
    //strarr[1] = node.SelectSingleNode("@Name").Value;
    list.Add(strarr);
    }
      

  13.   

    前辈!我试用了!不是很好!Item太多了!乱了!
      

  14.   

    menus里所有Item里的属性和Value值。 string path = @"a.xml";
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode("/Config/info/name");//code类似..
                    if (xn != null)
                        Console.WriteLine(xn.InnerXml);                XmlNode xn2 = doc.SelectSingleNode("/Config/Category/menus");
                    foreach (XmlNode xno in xn2.ChildNodes)
                    {
                        Console.WriteLine(xno.Attributes["Name"].Value.ToString());//xitong
                        Console.WriteLine(xno.InnerText);//系统
                    }
      

  15.   

    哈哈。2.0            string str = @"<Config>
      <info>
        <code>1</code>
        <name>1.0.0.0</name>
      </info>
      <Category>
        <menus>
          <Item Name=""XiTong"">系统</Item>
          <Item Name=""Bianji"">编辑</Item>
          <Item Name=""SoSuo"">搜索</Item>
          <Item Name=""ShiTu"">视图</Item>
          <Item Name=""Bangzhu"">帮助</Item>
        </menus>
      </Category>
    </Config>";            XmlDocument xml = new XmlDocument();
                xml.LoadXml(str);            //第一个
                string infocode = "";
                string infoname = "";
                {
                    XmlElement xe = (XmlElement)xml.SelectSingleNode("//info/code");
                    if (xe != null) infocode = xe.InnerXml;
                }
                {
                    XmlElement xe = (XmlElement)xml.SelectSingleNode("//info/name");
                    if (xe != null) infoname = xe.InnerXml;
                }            //第二个
                foreach (XmlElement xe in xml.SelectNodes("//Category/menus//Item"))
                {
                    //属性值
                    string name = xe.GetAttribute("Name");                //节点值
                    string inner = xe.InnerXml;
                }
      

  16.   

            private string GetNodeValue(string nodepath,string nodename)
            {
                string xmlFile = @"test.xml";
                XmlDocument doc = new XmlDocument();
                if (File.Exists(xmlFile))
                {
                    doc.Load(xmlFile);
                    XmlNode mainNode= doc.SelectSingleNode(nodepath);
                    if (mainNode[nodename] != null)
                        return mainNode[nodename].InnerText;
                }
                return "";        }
            private List<NameValue> GetNodeInfo(string nodepath, string nodename,string attrname)
            {
                List<NameValue> retLst = new List<NameValue>();
                string xmlFile = @"test.xml";
                XmlDocument doc = new XmlDocument();
                if (File.Exists(xmlFile))
                {
                    doc.Load(xmlFile);
                    XmlNode mainNode = doc.SelectSingleNode(nodepath);
                    if (mainNode == null)
                        return retLst;
                    if (mainNode.ChildNodes.Count > 0)
                    {
                        foreach (XmlNode node in mainNode.ChildNodes)
                        {
                            if (node.Name.Equals(nodename))
                            {
                                string value = node.InnerText;
                                string name="";
                                if (node.Attributes[attrname] != null)
                                    name = node.Attributes[attrname].Value;
                                NameValue info = new NameValue(name,value);
                                retLst.Add(info);
                            }
                        }
                    }
                }
                return retLst;
            }
        class NameValue
        {
            private string mName="";
            private string mValue="";
            public string Name
            {
                get{return mName;}
                set{mName=value;}
            }
            public string Value
            {
                get{return mValue;}
                set{ mValue=value;}
            }
            public NameValue(string name,string value)
            {
                mName=name;
                mValue=value;
            }
        }