Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From XmlDocument To XmlReader .Net

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) 
           ?????
like image 616
user1107078 Avatar asked Jan 26 '12 20:01

user1107078


People also ask

What's the difference between XmlDocument and XmlReader?

XmlDocument is very easy to use. Its only real drawback is that it loads the whole XML document into memory to process. Its seductively simple to use. XmlReader is a stream based reader so will keep your process memory utilization generally flatter but is more difficult to use.

Which method read XML documents from file URL for stream?

If your application needs to know which encoding is used to read the stream, consider using an XmlTextReader object to read the stream, and then use the XmlTextReader.

What is XmlNode in C#?

XmlNode is the base class in the . NET implementation of the DOM. It supports XPath selections and provides editing capabilities. The XmlDocument class extends XmlNode and represents an XML document. You can use XmlDocument to load and save XML data.


2 Answers

You can use

XmlReader xmlReader = new XmlNodeReader(xmlDoc);

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

like image 56
Wernight Avatar answered Oct 16 '22 19:10

Wernight


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?

like image 20
dblood Avatar answered Oct 16 '22 20:10

dblood