zoukankan      html  css  js  c++  java
  • 读取XML示例:C#获取XML的数据

    读取XML示例:C#获取XML的数据
    XML作为数据源来存储一些数据,那么如何获取XML中的数据呢?

    XML文件代码如下:

    1<?xml version="1.0" encoding="utf-8" ?>
     2<Tables>
     3  <Table Name="User_Info">
     4    <Field Name="User_ID">
     5      <Chinese>标识</Chinese>
     6      <English>ID</English>
     7      <Type>NUMBER(10)</Type>
     8      <CodeId></CodeId>
     9    </Field>
    10    <Field Name="User_Name">
    11      <Chinese>名称</Chinese>
    12      <English>Name</English>
    13      <Type>VARCHAR2(20)</Type>
    14      <CodeId></CodeId>
    15    </Field>
    16  </Table>
    17</Tables>

    下面是.cs文件核心读取XML数据代码:

     1protected void Button1_Click(object sender, EventArgs e)
     2        {
     3            XmlDocument doc = new XmlDocument();
     4            doc.Load(Server.MapPath("Reres.xml"));
     5            XmlNodeList nodes1 = doc.GetElementsByTagName("Table");
     6            foreach (XmlNode node1 in nodes1)    //第一层
     7            {
     8                if (node1.Attributes["Name"].Value == "User_Info")
     9                {
    10                    XmlNodeList nodes2 = node1.ChildNodes;
    11                    foreach (XmlNode node2 in nodes2)//第二层nodes1
    12                    {
    13                        if (node2.Attributes["Name"].Value == "User_ID")
    14                        {
    15                            TextBox1.Text += node2["Chinese"].InnerText;
    16                            TextBox2.Text += node2["English"].InnerText;
    17                            TextBox3.Text += node2["Type"].InnerText;
    18                        }
    19                    }
    20                }
    21            }
    22        }

    这么简单就可以获取XML中的数据

    =============================

    写法二:

    xml:

    Code
    Code

     

  • 相关阅读:
    SpringBoot实现原理
    常见Http状态码大全
    forward(转发)和redirect(重定向)有什么区别
    1094. Car Pooling (M)
    0980. Unique Paths III (H)
    1291. Sequential Digits (M)
    0121. Best Time to Buy and Sell Stock (E)
    1041. Robot Bounded In Circle (M)
    0421. Maximum XOR of Two Numbers in an Array (M)
    0216. Combination Sum III (M)
  • 原文地址:https://www.cnblogs.com/star250/p/1611407.html
Copyright © 2011-2022 走看看