Solved

Navigate/Query XmlDocument Created from Value of String

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now