In this example we work on the uses of IEnumerable Generics to capture the node name and values . The points to note that Values are taken as a string, elements can be reused within the rules that defined by XML

Keywords :

  • Create and Save XML with XDocument : StudentList.Save(@"C:\xml_linq\linqxml4\Student1.xml");

XDocument and XElement are derived from X container, various data types like String, Double, Integers, Dat-Time can be passed to these methods and these classes can be Enumerated.

Step : 1 Create a New Website and a Label on the Default.aspx page

Step 2: Codes

using System;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XDocument StudentList =
new XDocument(
new XElement("Students",
new XElement("Student",
new XElement("Name", "David"),
new XElement("Subject", "Chemistry"),
new XElement("Scores", "94", "96", "95", "92")),
new XElement("Student",
new XElement("Name", "Daniel"),
new XElement("Subject", "Chemistry"),
new XElement("Scores", "94", "98", "95", "96")),
new XElement("Student",
new XElement("Name", "Peter"),
new XElement("Subject", "Chemistry"),
new XElement("Scores", "94", "98", "95", "96"),
new XElement("Scores", "99"),
new XElement("Scores", "96"))
));
StudentList.Save(@"C:\xml_linq\linqxml4\Student1.xml");
L1.Text += "--<u><b>Displaying XML data StudentList.Elements('Students')</u> </b><br/>";
XElement root = StudentList.Element("Students");
IEnumerable<XElement> std = root.Elements();
foreach (XElement item in std)
{
XElement sname = item.Element("Name");
XElement ssub = item.Element("Subject");
XElement sscore = item.Element("Scores");

L1.Text += "&nbsp&nbsp<font color='Navy'>" + item.ToString() + "</font>";
L1.Text += "<br/> &nbsp&nbsp&nbsp&nbsp<font color='Red'>" + sname.Value + " , " + ssub.Value + " ," + sscore.Value + "</font>";

IEnumerable<XElement> sclist = item.Elements("Scores");
//L1.Text += "<br/> IEnumerable<XElement> sclist = item.Elements('Scores')<br/>";
foreach (XElement xsc in sclist)
{
L1.Text += "<br/>&nbsp&nbsp&nbsp<font color='Green'>&nbsp&nbsp" + xsc.Value +"</font>";
} L1.Text += "<br/>";
}
}
}
 

Step3 : Runtime analysis

Note the output on the second line, " L1.Text += StudentList.ToString();" holds  all the variables and prints all in a single string.

The image shows the location where xml file is saved, just as a token to show how to create and save an xml-document. The data displayed here used "StudentList" object than the file.

Here, the StudentList , an object of XDocument is enumerated as xml elements with "IEnumerable" into Elements operators, then sorted into individual "xml element" category using XElement class.

 

In the inner loop, we Enumerated item, that represents the data "Scores" as a repeated elements with changing value or values, in a for-each loop. The colored appeared these values are shown in Green.

The results in  "Navy" is generated by "item.ToString" , and the results in "Red" came from individual element values. We also noted that the element "Scores" has been used in redundancies ( many times), and the result of for-each loop is shown in green color.