Library tutorials & articles
Reading, Storing and Transforming XML Data in .NET
- Introduction
- The Code
- How it Works
- The Transformation
- Lastly, XML Elements and Nodes
Lastly, XML Elements and Nodes
Occasionally there may come a time when we simply need to read an XML file, only
retrieve an element or two and display it in a Drop Down List as opposed to a
DataGrid. How could we accomplish this? In addition, what if we had a nonconforming
atypical XML document with attributes, as opposed to our aforementioned Pubs XML
file, and we needed to read this information, how would we do this? Not that difficult
really, both can easily be accommodated with the XmlElement and XmlNodeList
Classes. Keep in mind however; there are numerous ways and methodologies within
.NET when working with XML. I happen to utilize these though. It 19;s primarily
dependent on your objective.
Whatever the case may be, to answer our first question we begin by using the
XmlDocument object to load our file, then call our XMLElement
object that represents the root XmlElement for our document, and
use the XMLNodeList, which represents all the nodes in our XML.
Since we only require the author's last name we use the XmlElement's
GetElementsByTagName property to locate that. Then we loop through
our entire XML file and by way of our Drop Down List Control's Items.Add
property we insert only the last names via InnerXml. Here's the code:
Dim XMLdoc as XmlDocument = new XmlDocument()
XMLdoc.Load(Server.MapPath("pubs.xml"))
Dim RootNode as XmlElement = XMLdoc.DocumentElement
Dim nodeList as XmlNodeList = RootNode.GetElementsByTagName("au_lname")
Dim i As Integer
For i = 0 To nodeList.Count - 1
authors.Items.Add (nodeList.Item(i).InnerXml)
Next i
'Clear our object
XMLdoc = Nothing
'... Our Drop Down List Server Control
<form runat="server">
<asp:DropDownList id="authors" runat="server" />
</form>
Now onto the second scenario. If you've actually examined our pre-created XML document "pubs.xml", you would've noticed it's well formed as follows:
<?xml version="1.0" standalone="yes"
?>
<PubsList>
<Table>
<au_id>409-56-7008</au_id>
<au_lname>Bennet</au_lname>
<au_fname>Abraham</au_fname>
<phone>415 658-9932</phone>
<address>6223 Bateman St.</address>
<city>Berkeley</city>
<state>CA</state>
<zip>94705</zip>
<contract>true</contract>
</Table>
<PubsList>
What we have here is the Root Node - PubsList, followed by the parent node - table and its node elements. If this XML for instance contained attributes in PubsList as listed below, how would we read these?
<PubsList Description="Contains a List of Authors">
To extract this information is very simple. After you load the document as we did with the XMLDocument object above, you'd again use the XMLElement's Attribute Property to get this value, like so:
Dim RootNode as XmlElement = XMLdoc.DocumentElement
Response.Write (RootNode.Attributes("description").Value)
In hindsight, although throughout this article we've examined a few ways that'll suffice for more common XML tasks, there still are a good number of XML objects available to read and parse XML documents that should be further looked into within the .NET documentation, primarily the QuickStart tutorial. All we've discussed should've at they very least peaked your interest.
Conclusion
In conclusion, we've covered some decent ground, that wasn't too hard, now
was it? You have a nice set of options for taking care of XML data. As aforementioned,
.NET offers tremendous options in "playing" with XML. I've presented
some commonly used ones, and upon further inquiries you'll encounter additional
ways of manipulating XML, like the XmlTextReader for more precise
XML parsing, XMLDataDocument object, the XPathDocument
object, and the XPathNavigator object for querying XML, to name
a few.
Therefore, from all aforementioned, it's quite clear that getting data from a database and displaying it from XML in ASP .NET couldn't be easier. This just happens to show the vast power that .NET has for the long haul in the web world.
Until next time, Happy NETing!
This article was originally published on DotNetJunkies
Related articles
Related discussion
-
handling special character in xslt's
by bussureddy82 (1 replies)
-
Reading an RSS file in ASP .NET
by TimL (31 replies)
-
Create a Site Search Engine in ASP.NET
by Soundguy53 (64 replies)
-
Cross page postbacks in ASP.NET 2.0
by PhilWhite (9 replies)
-
virtual directory asp.net
by yogeshkadvekar (3 replies)
Related podcasts
-
Writing FaceBook Applications with .NET - Interview with Mel Sampat, author of Outsync
In this episode, Scott talks with Mel Sampat, a Program Manager at Microsoft who's written OutSync, an application that syncs faces between Outlook, Facebook, and indirectly Windows SmartPhones. They chat about what it takes to write your own FaceBook application using ASP.NET or WinForms.
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
Hey Kurt,
The xsl file is the xml transformation file which parses the xml into more readable html.
i might be very slow here, but what part of the code here are supposed to be used in the pubs.xsl file? yes, I am a newbie :-)
Regards,
Kurt
Norway
Thanks - I'll check that and edit the article to fix it
Very good example, simple but contains a lot of information. Only one part I had to modify, in the XSL I had to change the criteria for the select from "PubsList/Pubs" to "PubsList/Table" as the child node is table and not pubs. I also had to grant write access to ASPNET account for the folder where I'm creating the XML.
Thanks,
Reem
This thread is for discussions of Reading, Storing and Transforming XML Data in .NET.