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
Comments