19

HI I have a xml document like this:

<Students>
<student name="A" class="1"/>
<student name="B"class="2"/>
<student name="c" class="3"/>
</Students>

I want to use XmlReader to read through this xml and return a list of students as List<student>. I know this can be achieved as follows:

 List<Student> students = new List<Student>();
    XmlReader reader = XmlReader.Create("AppManifest.xml");
    while (reader.Read())
    {
       if (reader.NodeType == XmlNodeType.Element && reader.Name == "student")
       {
            students.Add(new Student()
            {
                 Name = reader.GetAttribute("name"),
                 Class = reader.GetAttribute("Class")
             });
        }
     }

I just want to know if there is any better solution for this?

I am using silverlight 4. The xml structure is static, ie. it will have only one Students node and all the student node with above said attributes will only be there.

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Chinjoo
  • 2,697
  • 6
  • 28
  • 45

5 Answers5

53

Absolutely - use LINQ to XML. It's so much simpler:

XDocument doc = XDocument.Load("AppManifest.xml");
var students = doc.Root
                  .Elements("student")
                  .Select(x => new Student {
                              Name = (string) x.Attribute("name"),
                              Class = (string) x.Attribute("class")
                          })
                  .ToList();

XmlReader is a relatively low-level type - I would avoid it unless you really can't afford to slurp the whole of the XML into memory at a time. Even then there are ways of using LINQ to XML in conjunction with XmlReader if you just want subtrees of the document.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
8

It's alot easier if we're using Linq xml:

var xDoc = XDocument.Load("AppManifest.xml");

var students = 
    xDoc.Root.Elements("student")
    .Select(n =>
        new Student
        {
            Name = (string)n.Attribute("name"),
            Class = (string)n.Attribute("class"),
        })
    .ToList();
Torbjörn Hansson
  • 18,354
  • 5
  • 33
  • 42
1

Refer to the link below,

http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html

XmlReader is supported in Silverlight (http://msdn.microsoft.com/en-us/library/cc189001(v=vs.95).aspx)

XmlReader like a SQLDataReader is basically a non-cached model, so they are performance oriented but you can move only 'Forward' with that.

shA.t
  • 16,580
  • 5
  • 54
  • 111
Bharat Ram V
  • 101
  • 1
  • 3
1
List<Student> students = (from student in doc.Element("Students").Elements("student")
                          select new Student { 
                              Name = student.Attribute("name"), 
                              Class = student.Attribute("class") }
                          ).ToList();
Barry
  • 2,053
  • 3
  • 17
  • 28
0

You can use like that:

protected void Page_Load(object sender, EventArgs e)
{
    XmlTextReader reader = new XmlTextReader(Server.MapPath("~/XML"));

    while(reader.Read())
    {
        switch(reader.NodeType)
        {
            case XmlNodeType.Element:                            
                var lbl = new  Label();
                lbl.Text=(reader.Name + " -> " );
                break;

            case XmlNodeType.Text:                                 
                Response.Write(reader.Value + "<br />");
                 break;

            case XmlNodeType.EndElement:                        
                 Response.Write("</" + reader.Name + ">");
                 break;
        }
    }
}
shA.t
  • 16,580
  • 5
  • 54
  • 111