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


    开发过程中我们会遇到一些读取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);
    
                    }
                }
            }
        }
    }
    

      

     

  • 相关阅读:
    Shenzhen Wanze Technology Co., Ltd.技术支持
    Shenzhen Wanze Technology Co., Ltd.隐私协议
    123457123456#1#----com.MC.CarWashKidsGames234----前拼后广--洗车游戏mc-mc1111
    123457123457#0#----com.MC.konglongtianse222----前拼后广--恐龙填色mc-mc1111
    123457123456#5#----com.MC.HuaHuaGame866----前拼后广--babyDrawGame-mc555
    ios开发注意事项小总结
    ios开发将截图保存到相册
    iOS评分功能实现
    123457123456#2#----com.MC.DishuGame368----前拼后广--儿童打地鼠Game-mc2222222
    123457123457#0#----com.MC.3or1KongLongPT867----前拼后广--3or1恐龙PtGame-mc
  • 原文地址:https://www.cnblogs.com/feipengting/p/8986062.html
Copyright © 2020-2023  润新知