Solved

Navigate/Query XmlDocument Created from Value of String

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's the most efficient way to load up a couple of large table in C#? 5 45
Help with C#, MVC, razor. 6 34
Reset asp.net login password 4 42
Windows Media Player 2 26
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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