C# read XML file

xml file (test.xml)

<?xml version="1.0" encoding="utf-8" ?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>


C# code

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

namespace SearchEngineApplication
{
    public partial class XMLtest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BTXML_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("test.xml"));
            XmlNode root = doc.DocumentElement;
            Label1.Text = root.SelectSingleNode("to").ChildNodes[0].Value;
            Label2.Text = root.SelectSingleNode("from").ChildNodes[0].Value;
            Label3.Text = root.SelectSingleNode("heading").ChildNodes[0].Value;
            Label4.Text = root.SelectSingleNode("body").ChildNodes[0].Value;
        }
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章