讀取XML樣本:C#擷取XML的資料

來源:互聯網
上載者:User

讀取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
<?xml version="1.0" encoding="utf-8" ?>
<StarImages>
    <starimage>
        <speaker>猴子</speaker>
        <content>擺脫</content>
        <image>images/擺脫.gif</image>
    </starimage>

    <starimage>
        <speaker>祥龍</speaker>
        <content>爆發</content>
        <image>images/爆發.gif</image>
    </starimage>
    
</StarImages>

 

Code
private List<StarImage> ReadXML()
    {

        System.IO.FileInfo FileInfo = new System.IO.FileInfo(Server.MapPath("App_Data/XMLFile.xml"));
        List<StarImage> listsi=new List<StarImage>();


        if (FileInfo.Exists)
        { 
            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            xmlDocument.Load(FileInfo.FullName);
            foreach (System.Xml.XmlNode Node in xmlDocument["StarImages"])
            {
                StarImage si = new StarImage();
                si.speaker = Node.ChildNodes[0].InnerText;
                si.content = Node.ChildNodes[1].InnerText;
                si.image = Node.ChildNodes[2].InnerText;
                listsi.Add(si);
            }

        }

        return listsi;
    }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.