C# reads the xml file and writes it to the TreeView

During the development process, we will encounter some times when reading xml files. The following is the sorting out of my learning.

Read and load with XmlDocument

XmlDocument doc = new XmlDocument();
  doc.Load("XML.xml");//xml file path
/ / Get the root element of the XML to operate
XmlNodeList xn = doc.SelectNodes("countries/country");

 The complete code is as follows

front end:

<%@ 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 is loaded into 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="Follow the node" Value="Follow the node">
                    <asp:TreeNode Text="Child node 1" Value="Child node 1"></asp:TreeNode>
                    <asp:TreeNode Text="child node 2" Value="child node 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>

rear end:

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("/");
                // Load the XML file in
                XmlDocument doc = new XmlDocument();
                doc.Load("" + path3 + "XML.xml");
                / / Get the root element of the XML to operate
                XmlNodeList xn = doc.SelectNodes("countries/country");
                foreach (XmlNode xn1 in xn)
                { //Get the value of the first element text
                    string text = xn1.FirstChild.InnerXml;
                    XmlNode v = xn1.SelectSingleNode("value");
                    string re = v.InnerText;
                    //get property value
                    string val = v.Attributes["id"].Value;
                    Console.WriteLine(text + ":" + val + ":" + re);
                    //first level tree
                    TreeNode trerotnod = new TreeNode();
                    //Second level tree
                    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);

                }
                string pwd = Request.QueryString["c"];
            }
        }
    }
}

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325213199&siteId=291194637