Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XmlTextReader

I am a beginner programmer starting off with C#, and web services.

In the Service.cs file of my web service, I create a ReadXMLFile() method where I am trying to read an existing XML file, take the data from it and place it to the corresponding properties (DataMembers) that I created in the IService.cs file.

My problem is that my code is basically not doing anything. I've tried looking for web sites and tutorials on this but there really isn't much out there, especially for a beginner like myself. Anyone have any idea how I should go about this, because what I've been trying so far is obviously wrong.

Below is my ReadXMLFile() method.

void ReadXMLFile()
{
    XmlTextReader reader = new XmlTextReader("ClassRoll.xml");
    reader.Read();
    while (reader.Read())
    {
        if (reader.Name == "id")
        {
            id = reader.ReadString();
        }
        else if (reader.Name == "firstname")
        {
            link = reader.ReadString();
        }
        else if (reader.Name == "lastname")
        {
            description = reader.ReadString();
        }
        else if (reader.Name == "count")
        {
            description = reader.ReadString();
        }
        else if (reader.Name == "testscore")
        {
            description = reader.ReadString();
        }
    }
}

this is an example of my xml file

<classroll>
  <student>
    <id>101010</id>
    <lastname>Smith</lastname>
    <firstname>Joe</firstname>
    <testscores count="5">
      <score>65</score>
      <score>77</score>
      <score>67</score>
      <score>64</score>
      <score>80</score>
    </testscores>
  </student>
</classroll>
like image 358
Jess Avatar asked Apr 14 '12 03:04

Jess


People also ask

What is XmlTextReader?

XmlTextReader provides forward-only, read-only access to a stream of XML data. The current node refers to the node on which the reader is positioned. The reader is advanced using any of the read methods and properties reflect the value of the current node.

What is the difference between XmlReader and XmlTextReader?

XmlReader is the type you will use to create XML readers. XmlTextReader is one of the many implementations that XmlReader may use based upon what options you specify when creating the reader. Each reader has its own combination of features and abilities.


2 Answers

You're probably missing IsStartElement() condition in your while loop:

while (reader.Read())
{
    if (reader.IsStartElement())
    {
       if (reader.Name == "id")
       {
           id = reader.ReadString();
       }
...
}

Also, it would be easier to use XPath or LINQ to XML to read your XML, of course it depends on the file. Here are some examples: XPath and LINQ.

EDIT: after seeing XML file details

You should update your logic to keep track of current student and its testscores. Also, note that count is an attribute. It can get messy pretty soon, I suggest you take a look at the samples mentioned above.

like image 197
tenorsax Avatar answered Oct 15 '22 02:10

tenorsax


I think, that you get best result using XmlDocument

public void ReadXML()
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("<name file>.xml");
    xmlEntities = new List<XmlEntity>();

    foreach(XmlNode item in xmlDoc.ChildNodes)
    {
        GetChildren(item);
    }
}

private void GetChildren(XmlNode node)
{
    if (node.LocalName == "Строка")
    {
       //<you get the element here and work with it>
    }
    else
    {
       foreach (XmlNode item in node.ChildNodes)
       {
             GetChildren(item);
       }
    }
}
like image 36
Mikhail Rassudishkin Avatar answered Oct 15 '22 00:10

Mikhail Rassudishkin