C#读取XML文件

XML文件格式内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<DataSource xmlns="Company">
  <Node  UserName="公司1" UserID="G1" LevelID="1" ParentID="" ></Node>
  <Node  UserName="公司2" UserID="G2" LevelID="1" ParentID="" ></Node>
  <Node  UserName="公司3" UserID="G3" LevelID="1" ParentID="" ></Node>
  <Node  UserName="部门1" UserID="B1" LevelID="2" ParentID="G1" ></Node>
  <Node  UserName="部门2" UserID="B2" LevelID="2" ParentID="G1" ></Node>
  <Node  UserName="部门3" UserID="B3" LevelID="2" ParentID="G2" ></Node>
  <Node  UserName="科室1" UserID="K1" LevelID="3" ParentID="B1" ></Node>
  <Node  UserName="科室2" UserID="K2" LevelID="3" ParentID="B1" ></Node>
  <Node  UserName="科室3" UserID="K3" LevelID="3" ParentID="B3" ></Node>
  <Node  UserName="组1" UserID="Z1" LevelID="4" ParentID="K1" ></Node>
  <Node  UserName="组2" UserID="Z2" LevelID="4" ParentID="K1" ></Node>
</DataSource>

XML文件中每个Node节点有多个属性(UserName、UserID、LevelID、ParentID),当操作类似此格式的XML文件节点的属性时就可用到XmlNode类的Attributes属性

新建WebForm页面,添加TreeView控件TreeView1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Xml;//添加引用

namespace TestWeb
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TreeViewBind("", 1);
            } 
        }

        /// <summary>
        /// 绑定TreeView
        /// </summary>
        /// <param name="ParentID"></param>
        /// <param name="LevelID"></param>
        public void TreeViewBind(string ParentID, int LevelID)
        {
            XmlDocument doc = new XmlDocument();
            string xmlUrl = @"E:\Test.xml";
            doc.Load(xmlUrl);
            XmlNode xmlNode = doc.DocumentElement;
            TreeNode treeNode = new TreeNode(xmlNode.Attributes["xmlns"].Value);
            foreach (XmlNode xn in xmlNode)
            {
                if (xn.Attributes["ParentID"].Value == ParentID && xn.Attributes["LevelID"].Value == LevelID.ToString())
                {
                    TreeNode node = new TreeNode();
                    node.Text = xn.Attributes["UserName"].Value;
                    node.Value = xn.Attributes["UserID"].Value;
                    NodeBind(node, xn.Attributes["UserID"].Value, Int32.Parse(xn.Attributes["LevelID"].Value) + 1);
                    treeNode.ChildNodes.Add(node);
                }
            }
            TreeView1.Nodes.Add(treeNode);
        }
    
        /// <summary>
        /// 递归绑定
        /// </summary>
        /// <param name="CurrentNode"></param>
        /// <param name="ParentID"></param>
        /// <param name="LevelID"></param>
        private void NodeBind(TreeNode CurrentNode, string ParentID, int LevelID)
        {
            XmlDocument doc = new XmlDocument();
            string xmlUrl = @"E:\Test.xml";
            doc.Load(xmlUrl);
            XmlNode xmlNode = doc.DocumentElement;
            foreach (XmlNode xn in xmlNode)
            {
                if (xn.Attributes["ParentID"].Value == ParentID && xn.Attributes["LevelID"].Value == LevelID.ToString())
                {
                    TreeNode childnode = new TreeNode();
                    childnode.Text = xn.Attributes["UserName"].Value;
                    childnode.Value = xn.Attributes["UserID"].Value;
                    NodeBind(childnode, xn.Attributes["UserID"].Value, Int32.Parse(xn.Attributes["LevelID"].Value) + 1);
                    CurrentNode.ChildNodes.Add(childnode);
                }
            }
        }
    }
}

方法二:

linq to xml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Xml.Linq;//添加引用

namespace TestWeb
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TreeViewBind("", 1);
            }
        }

        public void TreeViewBind(string ParentOrgCode, int LevelNo)
        {
            XElement XE = XElement.Load(@"E:\Test.xml");
            TreeNode treenode = new TreeNode(XE.Attribute("xmlns").Value);
            var query = from xe in XE.Elements()
                        where xe.Attribute("ParentID").Value == ParentOrgCode && xe.Attribute("LevelID").Value == LevelNo.ToString()
                        select xe;
            foreach (XElement x in query)
            {
                TreeNode node = new TreeNode();
                node.Text = x.Attribute("UserName").Value;
                node.Value = x.Attribute("UserID").Value;
                NodeBind(node, x.Attribute("UserID").Value, Int32.Parse(x.Attribute("LevelID").Value) + 1);
                treenode.ChildNodes.Add(node);

            }
            TreeView1.Nodes.Add(treenode);
        }


        /// <summary>
        /// 递归绑定
        /// </summary>
        /// <param name="CurrentNode"></param>
        /// <param name="ParentOrgCode"></param>
        /// <param name="LevelNo"></param>
        private void NodeBind(TreeNode CurrentNode, string ParentOrgCode, int LevelNo)
        {
            XElement XE = XElement.Load(@"E:\Test.xml");
            var query = from xe in XE.Elements()
                        where xe.Attribute("ParentID").Value == ParentOrgCode && xe.Attribute("LevelID").Value == LevelNo.ToString()
                        select xe;
            foreach (XElement x in query)
            {
                TreeNode childnode = new TreeNode();
                childnode.Text = x.Attribute("UserName").Value;
                childnode.Value = x.Attribute("UserID").Value;
                NodeBind(childnode, x.Attribute("UserID").Value, Int32.Parse(x.Attribute("LevelID").Value) + 1);
                CurrentNode.ChildNodes.Add(childnode);
            }
        }
    }
}





posted on 2012-05-17 14:02  java课程设计例子  阅读(185)  评论(0编辑  收藏  举报