+ -
当前位置:首页 → 问答吧 → 小妹请教读取XML节点的属性、值问题。

小妹请教读取XML节点的属性、值问题。

时间:2011-12-12

来源:互联网

我有一个XML,我想写两个方法。

一个方法得到info里两个节点code、和name的值。
一个方法是得到menus里所有Item里的属性和Value值。

XML code

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



非常感谢各位!

作者: wangtiantian23   发布时间: 2011-12-12

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

作者: hefeng_aspnet   发布时间: 2011-12-12

我先尝试一下啊!

作者: wangtiantian23   发布时间: 2011-12-12

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

作者: Arestain   发布时间: 2011-12-12

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

作者: jubobo   发布时间: 2011-12-12

C# code


                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

作者: wangtiantian23   发布时间: 2011-12-12

能不能有种方法我直接历遍/Config/info 和 /Config/Category/menus

分别从这两个节点开始历遍这样寻找会准确些!

作者: wangtiantian23   发布时间: 2011-12-12

C# code

        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已经在内存中,例如是一个字符串,可以这样进行读取
C# code

XElement x = XElement.Parse("<?xml version=...");

作者: sdfkfkd   发布时间: 2011-12-12

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

不好意思!

作者: wangtiantian23   发布时间: 2011-12-12

特意冲妹子进来看看
给你个流氓的写法
C# code

DataSet ds =new DataSet();
ds.ReadXml(你的文件或流);

DataTable info =ds.Tables["info"];
DataTable menus=ds.Tables["Item"];



拼写可能有错误,纯手打 王甜甜同学

作者: cancerser   发布时间: 2011-12-12

怎么这么复杂..直接这样就可以啊..其他节点类似..
C# code

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

作者: enter89   发布时间: 2011-12-12

C# code

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


作者: sdl2005lyx   发布时间: 2011-12-12

我这样做的成功了!
C# code

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

作者: wangtiantian23   发布时间: 2011-12-12

用Xpath获取数据,发现很多人都不会。难得6楼的兄弟还记得

C# code
        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);
        }

作者: gzdiablo   发布时间: 2011-12-12

用dom4g sax dom ...

作者: aliuli   发布时间: 2011-12-12