C#读取xml文件写入到TreeView中

时间:2023-01-14 00:11:31

开发过程中我们会遇到一些读取xml文件的时候,下面是我学习的整理。

用XmlDocument读取加载

 XmlDocument doc = new XmlDocument();
  doc.Load("XML.xml");//xml的文件路径
 //获取到XML的根元素进行操作
XmlNodeList xn = doc.SelectNodes("countries/country");

 完整代码如下

前端:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demo0427.aspx.cs" Inherits="WebApplication2.demo0427" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>xml加载到tree中</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
            <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
            <Nodes>
                <asp:TreeNode Text="跟节点" Value="跟节点">
                    <asp:TreeNode Text="子节点1" Value="子节点1"></asp:TreeNode>
                    <asp:TreeNode Text="子节点2" Value="子节点2"></asp:TreeNode>
                </asp:TreeNode>
            </Nodes>
            <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px" VerticalPadding="0px" />
        </asp:TreeView>
    </div>
    </form>
</body>
</html>

后端:

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

namespace WebApplication2
{
    public partial class demo0427 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string path3 = Server.MapPath("/");//获得当前的路径
                //将XML文件加载进来
                XmlDocument doc = new XmlDocument();
                doc.Load("" + path3 + "XML.xml");
                //获取到XML的根元素进行操作
                XmlNodeList xn = doc.SelectNodes("countries/country");
                foreach (XmlNode xn1 in xn)
                {   //获取第一个元素text的值
                    string text = xn1.FirstChild.InnerXml;
                    XmlNode v = xn1.SelectSingleNode("value");
                    string re = v.InnerText;
                    //获取属性值
                    string val = v.Attributes["id"].Value;
                    Console.WriteLine(text + ":" + val + ":" + re);
                    //第一级树
                    TreeNode trerotnod = new TreeNode();
                    //第二级树
                    TreeNode x = new TreeNode();
                    TreeNode y = new TreeNode();
                    trerotnod.Text = xn1.InnerText;
                    y.Text = val;
                    x.Text = re;
                    //把第二级的树添加到第一级的树中
                    trerotnod.ChildNodes.Add(x);
                    trerotnod.ChildNodes.Add(y);
                    TreeView1.Nodes.Add(trerotnod);

                }
            }
        }
    }
}