From XmlDocument To XmlReader .Net

user1107078 picture user1107078 · Jan 26, 2012 · Viewed 41.1k times · Source

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) 
           ?????

Answer

Wernight picture Wernight · Oct 5, 2012

You can use

XmlReader xmlReader = new XmlNodeReader(xmlDoc);

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