This document resembles the previous one, where we used XMLDocument rather than using a TextReader.

  • Keywords: Load, Save, XElement, TextReader, StringBuilder. Traversing xml file

In this example we are doing the following operations

  • Creating XML using Text reader
    • Loaded on an instance ( xe ) of XElement class
      • Saved instance xe in the hard-drive
  • An instance of XElement class (studentxml) is then loaded with the mapped xml file.
    • This studentxml is Enumerated with Generic Collection and fetch a for a loop, as shown in the
      illustration below. This operation is a deferred operation, as the loop spins, the values are loaded
      in a sequence like Student's name, subject and semesters.

Step: 1 Create new web site, add three Label controls and codes as given below.

Step2: Codes

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>mm.LINQXML5: Uses of TextReader, XElement,Save </title>
<style type="text/css">
.style1
{
color: #800000;
}
.style2
{
}
.style3
{
width: 367px;
}
.style4
{
width: 414px;
}
</style>
</head>
<body style="background-color: #C0C0C0">
<form id="form1" runat="server">
<div style="padding: 4px; margin: auto; background-color: #FFFFCC; border: 2px solid #800000">
<span class="style1">
<u>Uses of TextReader tr = new StringReader< XElement >&lt;XElement&gt;</u></span>
<table style="width: 65%;">
<tr>
<td class="style2" colspan="2"><asp:LabelID="L3" runat="server" Text=""></asp:Label></td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class="style4"><asp:LabelID="L1" runat="server" Text=""></asp:Label></td>
<td class="style3"><asp:Label ID="L2" runat="server" Text=""></asp:Label></td>
<td>
&nbsp;</td>
</tr>
</table>
</div>
</form>
</body>
</html>
 

Code: Default.aspx.cs

using System.IO;
using System;
using System.Text;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//StringBuilder sb = new StringBuilder();
TextReader tr = new StringReader(@"
<Students>
<Student>
<Name>David </Name>
<Subject>Chemistry </Subject>
<Sem1> 94</Sem1>
</Student>
<Student>
<Name> Daniel</Name>
<Subject> Chemistry</Subject>
<Sem1> 98</Sem1>
</Student>
<Student>
<Name>Peter </Name>
<Subject> Chemistry</Subject>
<Sem1> 97</Sem1>
<Sem2>96 </Sem2>
</Student>
<Student>
<Name>Vishal </Name>
<Subject> History</Subject>
<Sem1> 95</Sem1>
<Sem2>96 </Sem2>
</Student>

</Students>");
L3.Text += "Using tr.ToString() : " + tr.ToString();
XElement xe = XElement.Load(tr);
//Can't be read twice
// XElement xe2 = XElement.Load(tr);
tr.Close();
try
{
xe.Save(@"C:\xml_linq\linqxml5\Student1.xml");
//xe2.Save(@"C:\xml_linq\Student2.xml");
}
catch(Exception error)
{
L1.Text += error.Message.ToString();
}
//Now load this xml document with XElement
XElement studentxml = null;
XDocument stddoc = null;
L3.Text += "<br/>Loading Student1.xml <br/>";
studentxml = XElement.Load(@"C:\xml_linq\linqxml5\Student1.xml");
IEnumerable<XElement> query1 = from std in studentxml.Elements("Student") select std;
var query2 = from std in studentxml.Elements("Student") select std;
L1.Text += "<br/><u><b> Using IEnumerable Query </b></u></br/>";
foreach (XElement item in query1)
{
L1.Text += "&nbsp"+item.ToString();
L1.Text += "<br/>";
}
L2.Text += "<br/><u><b> Using var </b></u></br/>";
foreach (XElement item in query2)
{
L2.Text += "&nbsp <font color='red'>" + item.ToString();
L2.Text += "</font><br/>";
}

}
}

 

Step: 3. Note, TextReaderin this example, (in contrast to xml-document shown in previous example LINK), reads data from a saved file, and the second line shows the default properties.

In the next steps, the file saved on the hard-disk is loaded using XElement class and enumerated either with IEnumerable class or var object.