Solved

Navigate/Query XmlDocument Created from Value of String

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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