Blog

Different ways of reading an XML file

Posted by Suthish Nair Blogs | XML Dec 17, 2010
Different ways of reading an XML file
...code snippets...


using System.Xml;
using System.Linq;
using System.Xml.Linq;
Response.Write("<b>Using XmlTextReader</b><br /><br />");
using (XmlTextReader reader = new XmlTextReader(Server.MapPath("Country.xml")))
{
 while (reader.Read())
 {
  if (reader.NodeType == XmlNodeType.Text)
  {
   Response.Write(reader.Value + "<br />");
  }
 }
}
Response.Write("<br /><b>Using XmlReader</b><br /><br />");
using (XmlReader reader = XmlReader.Create(Server.MapPath("Country.xml")))
{
 while (reader.Read())
 {
  if (reader.NodeType == XmlNodeType.Text)
  {
   Response.Write(reader.Value + "<br />");
  }
 }
}
Response.Write("<br /><b>Using XmlNodeReader</b><br /><br />");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("Country.xml"));
XmlNodeReader nodeReader = new XmlNodeReader(xmlDoc);
while (nodeReader.Read())
{
 if (nodeReader.NodeType == XmlNodeType.Text)
 {
  Response.Write(nodeReader.Value + "<br />");
 }
}
xmlDoc = null; nodeReader = null;
Response.Write("<br /><b>Using XDocument</b><br /><br />");
XDocument xDoc = XDocument.Load(Server.MapPath("Country.xml"));
var xmlAtt = from att in xDoc.Elements("CountryList").Elements("Name")
    select att;
foreach (var record in xmlAtt)
{
 Response.Write(record.Value + "<br />");
}
xDoc = null; xmlAtt = null;
COMMENT USING
PREMIUM SPONSORS
Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.
Employers - Post Free Jobs
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.