Scott on Writing

Musings on technical writing...

Working with XML Strings

Pop-quiz, hotshot: you have received a payload of XML data in the form of a string from, say, a Web service. Now, you want to perhaps parse this XML data, and perhaps display it in a DataGrid. What do you do, hotshot, what do you do?

Ideally, it would be nice to be able to load the data into one of the many XML-related classes the .NET Framework BCL provides, perhaps the XmlDocument. XmlDocument contains a Load() method, so you think to yourself, "Ok, here's what I'll do:"

// Create a new XmlDocument instance...
XmlDocument xmlDoc = new XmlDocument();

// Load the XML string data...
xmlDoc.Load("Mitchell");

That won't work, though. The XmlDocument's Load() will accept a string input parameter, but it expects it to be either a URL or a file path where the XML data can be found. Drag. The Load() method, however, can also accept a TextReader instance. Therefore, we can load the XML string data into a StringReader instance, (which is derived from the TextReader class), and then pass in this StringReader into the Load() method!

// Create and populate a StringReader StringReader
sr = new StringReader("Mitchell");

// Create a new XmlDocument instance...
XmlDocument xmlDoc = new XmlDocument();

// Load the XML string data...
xmlDoc.Load(sr);

I recently wrote up a FAQ discussing this in more detail. Also worth checking out is this article by Anthony Hart: DataGrids, DataSets, and XML Strings.

Happy Programming!

Update
A bit of searching the docs has revealed that the XmlDocument class contains a LoadXml() method that loads from XML string data.  So... I guess you could use that (assuming you don't need to validate the XML upon loading - for that you still need to use the Load() method).  :-)  However, for things like XmlTextReaders and whatnot, you will still need to use StringReaders for loading XML string data....

posted on Thursday, October 16, 2003 10:29 AM

Feedback

# re: Working with XML Strings 10/16/2003 11:07 AM Adam

Or you just use the XmlDocument method that is intended to work with XML strings:

XmlDocument.LoadXml(string xml)

Put your XML into that method and it'll construct the document for you, without going through the rigamarole of creating the stream classes manually.

# re: Working with XML Strings 10/16/2003 11:07 AM Adam

Hm, I swear that update wasn't there when I started commenting. :)

# re: Working with XML Strings 10/16/2003 11:09 AM bliz

So how do you handle it when your XML data is 3-levels deep instead of a relational-friendly 2-levels deep? I go at it the old fashioned way of walking the document and parsing the string, manually building a dataset (with relationships). Otherwise (i.e., letting XmlDataDocument / DataSet do their own thing), the relationship between level 1 and level 3 is completely lost inside the dataset. (bummer)

# re: Working with XML Strings 10/16/2003 12:27 PM James Avery

Even better would be to avoid using the XmlDocument at all and stick with the XPathNavigator object.

-James

# re: Working with XML Strings 12/2/2003 8:24 AM matt

I'm currently using the LoadXML method with a string and the .aspx page has a ContentType of "Text/XML". How do I show the xml data? Is there a "write" method somewhere?

# re: Working with XML Strings 12/2/2003 10:04 AM Scott Mitchell

Matt, check out this blog entry:
http://scottonwriting.net/sowblog/posts/361.aspx

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<July 2008>
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Comment Stats

DayTotal% of Total
Sunday 1896.8%
Monday 39014.0%
Tuesday 46916.8%
Wednesday 51518.5%
Thursday 54419.5%
Friday 50818.2%
Saturday 1706.1%
Total 2785100.0%

Hour1Total% of Total
12:00 AM 682.4%
1:00 AM 712.5%
2:00 AM 632.3%
3:00 AM 752.7%
4:00 AM 572.0%
5:00 AM 1093.9%
6:00 AM 1114.0%
7:00 AM 1615.8%
8:00 AM 1756.3%
9:00 AM 1505.4%
10:00 AM 1736.2%
11:00 AM 1826.5%
12:00 PM 1906.8%
1:00 PM 1766.3%
2:00 PM 1605.7%
3:00 PM 1324.7%
4:00 PM 1124.0%
5:00 PM 983.5%
6:00 PM 913.3%
7:00 PM 993.6%
8:00 PM 853.1%
9:00 PM 802.9%
10:00 PM 833.0%
11:00 PM 843.0%
Total 2785100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.14144
Monday 5.35353
Tuesday 4.35444
Wednesday 7.58644
Thursday 6.87625
Friday 5.45414
Saturday 5.03161
Total 5.802785

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.45109
9:00 AM 6.34279
10:00 AM 6.41250
11:00 AM 4.28184
12:00 PM 6.98342
1:00 PM 2.87112
2:00 PM 5.29222
3:00 PM 8.54299
4:00 PM 3.9190
5:00 PM 5.78156
6:00 PM 4.52113
7:00 PM 9.32177
8:00 PM 9.06154
9:00 PM 5.14113
10:00 PM 6.2381
11:00 PM 4.5732
Total 5.802785

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles