MIME-Version: 1.0 Content-Location: file:///C:/5043B227/xml_reading.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="us-ascii" using System;

using System;

using System.Collections;

using System.ComponentModel;=

using System.Data;

using System.Drawing;

using System.Web;=

using System.Web.SessionStat= e;

using System.Web.UI;

using System.Web.UI.WebContr= ols;

using System.Web.UI.HtmlCont= rols;

using System.IO;<= /span>

using System.Xml;=

 

namespace XMLtoDataset

{

       /= // <summary>

       /// Summary descrip= tion for WebForm1.

       /= // </summary>

       public class<= /span> WebForm1 : System.Web.UI.Page

       {

      

            =   private void Page_Load(objec= t sender, System.EventArgs e)

            =   {

            =          //TODO:Could we cache the topics file? Then you would= n't have to access it from

            =          //disk all the time.

            =          string topicName;

 

            =          GetSupertopicID("6789");

 

            =          topicName =3D GetTopicName("4567");

 

            =          Response.Write(topicName);

            =   }

 

            =   public void GetSupertopicID(string id)

            =   {

            =          string currentTopicI= D;

            =          XmlDocument doc;

 

            =          //Load the topics.xml file.<= /p>

            =          //TODO: This should be referenced in web.config.=

            =          string xmlFileUrl;

            =          xmlFileUrl =3D "http://localhost/xmltodataset/tony.xml";

 

            =          //Call the function to load the XmlDocument.

            =          doc =3D LoadTopicsFile(xmlFileUrl);<= /p>

            =          <= /p>

            =          //Get the root node and the first= topic node.

            =          XmlNode root =3D doc.DocumentElement;

            =          XmlNode topic;

            =          XmlNode supertopic;

 

            =          //Select the topic node with the matching attribute value.

            =          topic =3D root.SelectSingleNode("descendant::topic[id=3D'" + id + "']");

            =          supertopic =3D topic.SelectSingleNode("descendant::supertopic");

 

            =          //Response.Write(topic.Las= tChild.InnerText);

            =          Response.Write(supertopic.InnerText + "<br>");

            =   }

            =  

            =   //id is the id you are trying to get the name for.

            =   public string GetTopicName(= string id)

            =   {

            =          string name;

            =          XmlDocument doc;

 

            =          //Load the topics.xml file.<= /p>

            =          //TODO: This should be referenced in web.config.=

            =          string xmlFileUrl;

            =          xmlFileUrl =3D "http://localhost/xmltodataset/tony.xml";

 

            =          //Call the function to load the XmlDocument.

            =          doc =3D LoadTopicsFile(xmlFileUrl);<= /p>

 

            =          //Get the root node and the first= topic node.

            =          XmlNode root =3D doc.DocumentElement;

            =          XmlNode topic;

            =          XmlNode nameNode;

 

            =          //Select the topic node with the matching attribute value.

            =          topic =3D root.SelectSingleNode("descendant::topi= c[id=3D'" + id + "']");

            =          nameNode =3D topic.SelectSingleNode("descendant::name");

 

            =          name =3D nameNode.InnerText;

 

            =          return name;

            =   }

 

            =  

            =   public XmlDocument LoadTopicsFile(string url)

            =   {

            =          //Create an XML document and load the topics.xml file= .

            =          //If it does not load properly th= row an exception.

            =          XmlDocument doc =3D new XmlDocume= nt();

            =          try

            =          {=

            =             &nb= sp;  doc.Load(url);

            =          }=

            =          catch

            =          {=

            =             &nb= sp;  Exception exe =3D new Exception= ("Could not load the topics XML file " +

            =             &nb= sp;         url + ".");

            =             &nb= sp;  throw exe;

            =          }=

            =          return doc;

            =   }

 

        &= nbsp;     #region Web Form Designer gene= rated code

            =   override protected void OnInit(EventArgs e)

            =   {

            =          //

            =          // CODEGEN: This call is required by the ASP.NET Web = Form Designer.

            =          //

            =          InitializeComponent();

            =          base.OnInit(e);=

            =   }

            =  

            =   /// <summary>

            =   /// Required method for Designer support - do not modify

            =   /// the contents of this method with the code editor.

            =   /// </summary>

            =   private void InitializeComponent()

            =   {   

            =          this.Load +=3D new System.EventHandler(this.Page_Load);

 

            =   }

        &= nbsp;     #endregion

       }

}