Blog

Most Efficient Way to Read XML

Posted by Jaijo Paulose Blogs | XML Jan 03, 2010
Being a Microsoft technology developer, Reading XML is a very basic task, to read. Write or transfer data. Whether it be RSS feeds or Database data access, everywhere its all XML , There are a lot of ways to read XML using XPATH language XPATHNAVIGATOR, browsing through each nodes and finally extracting data. This is so complex and of course lot of coding. I have suggested an easy and efficient way to READ XML

------------ Coding is all what I know -------------

Being a Microsoft technology developer, Reading XML is a very basic task, to read. Write or transfer data. Whether it be RSS feeds or Database data access, everywhere its all XML , There are a lot of ways to read XML using XPATH language XPATHNAVIGATOR, browsing through each nodes and finally extracting data. This is so complex and of course lot of coding. I have suggested an easy and efficient way to READ XML  

String xmlURL = “http://www.microsoft.com/feeds/msdn/en-us/rss.xml “;     // Specify any RSS Feed Url

             XmlReader xReaderResult = null;

                try

                {

                    xReaderResult = XmlReader.Create(xmlUrl, settings);

                    doc.Load(xReaderResult); // Loading the XML Document

                }

                catch (XmlException ex)

                {

                    throw new Exception("error RSS feed is not well formed: " + ex.Message);

                }

 

                XmlNode nRoot = doc.DocumentElement;

                XmlNodeList xNodeList = nRoot.SelectNodes("channel/item"); // Selecting the Nodes in XML in this case for RSS feeds

 

                XmlTextReader xReader;

                // Dumping the RSS Data into Datatable

                for (int x = 0; x < xNodeList.Count; x++)

                {

                    xReader = new XmlTextReader(xNodeList.Item(x).OuterXml, XmlNodeType.Element, new XmlParserContext(null, null, null, XmlSpace.None));

                    ds.ReadXml(xReader, XmlReadMode.InferSchema);

                }

 

                dt = ds.Tables[0];

 

             dt.TableName = this.Title;

 

After Getting the XML data into the Datatable you can extract the data and get each of the node data or bind it to a Grid View and display the data in Grid Fashion

 

Extracting the XML data from datatable

 

                              int k = 0;

                       foreach( Datarow oDr in dt.Rows)

        {

           titleArray[k] = oDr[“Title”]; // Extract each Node

                  pubdateArray[k] = oDR[“pubdate”];

           k++;

        }

 

Binding it to Gridview

 

                oGridView.Bind(dt);

 

Thanks Enjoy !!!!

Joe

post comment
     

XML is great.  And XMLTextReader(s) open up the doors to integrating with link, doing something like this:

XElement linqDataSet = XElement.Load(XML_TEXT_READER_OBJECT);
IEnumerable<XElement> returnVal =
   (
     from localSet in linqDataSet.Descendants("TABLE_IN_XML_SET")
     select localSet
   );

Posted by Tim Claason Jan 08, 2010

I think using XDocument and XElement would be more efficient, It allows us to use LINQ API power as well.

Posted by Tarik Jan 05, 2010
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
More Blogs from this Blogger
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts