Solved

Navigate/Query XmlDocument Created from Value of String

Posted on 2003-10-24
3
253 Views
Last Modified: 2010-08-05
Hi All,
I am creating an xml document based on the results of a SQL query, as follows:

StringReader sr = new StringReader(strSQLResult);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(sr);
XmlTextReader textReader  = new XmlTextReader(sr);
textReader.Read();

When I get to the Read(), I get an error saying, "System.Xml.XmlException: The root element is missing." However, if I do a Response.Write of the value of strSQLResult to the browser, the browser renders the xml file without errors.

strSQLResult is a string created by iterating through the results of a query that have been returned to a DataReader. The value of strSQLResult is like (without formatting):
<Prod>
   <Row>
      <ID>1</ID>
      <Name>Division</Name>
   </Row>
   <Row>
      <ID>2</ID>
      <Name>Brand</Name>
   </Row>
</Prod>

Can anyone tell me how I can now navigate/query the XmlDocument object? I need to pass through it several times to populate different DropDownLists. Any specific information and/or references to online resources illustrating how to handle this would be most appreciated.

TIA,
Sven
0
Comment
Question by:Thermidor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
purpleblob earned 250 total points
ID: 9674163
One way to query the XML document is by using XPath.

This can be done by calling the SelectNodes method and parsing in an XPath string

System.Xml.XmlNodeList nodes = xmldoc.SelectNodes("//row/");

or you can use GetElementsByTagName which allows you to get all elements with a certain tag name

or you could iterate through the document contained within the DOM by getting the ChildNodes and then iterating through each Node with the returned NodeList

Which method you use depends upon what you specifically need to do or what you need to get from your document

Hope this helps
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question