Solved

Navigate/Query XmlDocument Created from Value of String

Posted on 2003-10-24
3
251 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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