22

After an advice from a user that answered to my question I'm tring to convert my XmlDocument code to XmlReader code but I'm having some troubles.

This is XML (generated from php-mysql Page)

<row>
<idLink>64</idLink>
<idHost>3</idHost>
<url>http://www.google.com</url>
</row>
<row>
<idLink>68</idLink>
<idHost>4</idHost>
<url>http://www.bing.com</url>
</row>
..... until about 10000 rows

This is my XmlDocument code:

   xmlDoc.Load("http://www.myUrl.com/list.php");
      if (xmlDoc.DocumentElement != null){
          foreach (XmlNode node in xmlDoc.DocumentElement)
             {
              if (node.Name == "row")
                {
                  list.Add(new Links { 
                       idLink = Convert.ToInt32(node.ChildNodes[0].InnerText),
                       idHost = Convert.ToInt32(node.ChildNodes[1].InnerText),
                       url = node.ChildNodes[2].InnerText }); 
                  }
             }  
             return list;

Now I have some trouble to Convert in XmlReader, I tried many code but I can't handle it.

using (XmlReader reader = new XmlTextReader("http://myUrl.com/list.php"))
         { 
          if (reader.NodeType == XmlNodeType.Element) 
           ?????
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
user1107078
  • 455
  • 1
  • 7
  • 18
  • 1
    Trying to (robustly) read xml with XmlReader is, frankly, hard. What is it that you are trying to do here? what is your objective? and what is the size of the xml? – Marc Gravell Jan 26 '12 at 20:51
  • @MarcGravell The size of xml is , as i wrote in the xml code , about 10000 rows. I'm reading a xml e put in a List. – user1107078 Jan 26 '12 at 20:58
  • What fails with XmlDocument? Have you tried XmlSerializer? XElement? – Marc Gravell Jan 26 '12 at 20:59
  • @MarcGravell the problem is i need to keep in memory this Object List and they are about 10000. As i read in this article http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html the performance are better with XmlReader – user1107078 Jan 26 '12 at 21:02
  • @MarcGravell The problem is the Ram and Pc's. They are (most of them) Virtual Machine with 512 Mb Ram dedicated and i would increase performance. – user1107078 Jan 26 '12 at 21:13

2 Answers2

83

You can use

XmlReader xmlReader = new XmlNodeReader(xmlDoc);

See: http://blog.jongallant.com/2007/01/convert-xmldocument-to-xmlreader.html

hal
  • 1,705
  • 1
  • 22
  • 28
Wernight
  • 36,122
  • 25
  • 118
  • 131
8

If you are doing read only operations on an xml file then you can you use XmlReader but as @Marc Gravell points out it is difficult.

In this situation I will create a class that wraps an XPathDocument using an XmlReader. I then create an XPathNavigator to read the data. Here's an example:

public class MyXmlReader
{
    public MyXmlReader(string xml)
    {
        StringReader sReader = new StringReader(xml);

        XPathDocument xml = new XPathDocument(XmlReader.Create(sReader));

        xmlNav = xml.CreateNavigator();
    }

    private XPathNavigator xmlNav = null;


    public MyDataModel ReadMyDataModel()
    {
        MyDataModel model = new MyDataModel();

        model.Read(xmlNav);

        return model;
    }
}

As shown above, the reading of the data can then be encapsulated into an associated object model. You can see some details in my answer on this question:

How do I manipulate an XML document one parent element at a time?

Community
  • 1
  • 1
dblood
  • 1,758
  • 17
  • 21
  • ok, let me know if you have any more questions. I use this method frequently for read only access to xml files. – dblood Jan 26 '12 at 21:38