读取XML获取属性

前端之家收集整理的这篇文章主要介绍了读取XML获取属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
</pre><pre name="code" class="csharp">        protected void init()
        {
            string filename = "Student.xml";
            DataSet ds = new DataSet();
            ds.ReadXml(filename);
            dataGridView1.DataSource = ds.Tables[0];

            //--------指定查询--------
            string name = "王亚晓";
            XmlDocument doc = new XmlDocument();
            doc.Load(filename);
            XmlNode root = doc.SelectSingleNode("student");
            XmlNode target = root.ChildNodes[0];

            foreach (XmlNode child in root.ChildNodes)
            {
                foreach (XmlNode grandson in child.ChildNodes)
                {
                    if(grandson.Name=="name"&&grandson.InnerText==name)
                    {
                        target = grandson.ParentNode;
                    }
                }
            }
            Console.WriteLine("name:"+target.ChildNodes[0].InnerText);
            Console.WriteLine("age:"+target.ChildNodes[1].InnerText);
            Console.WriteLine("sex"+target.ChildNodes[2].InnerText);
        }

//获取属性
                Console.WriteLine(child.Attributes["type"].Name);
                Console.WriteLine(child.Attributes["type"].Value);

猜你在找的XML相关文章