BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


.NET and XML
Pass node sets to XSLT stylesheets using .NET
Output a directory as XML using .NET
.Net, COM Interoperability and Word Object Model
WS-Routing for .NET endpoints
Using XML and XSLT in VB.NET Jeopardy
Symmetric Encryption/Decryption with .NET
XInclude.NET 1.0
EXSLT.NET 1.0
How to read a XML file using XmlTextReader?
How to compare a XML element with XmlTextReader?
How to verify if an element is empty with XmlTextReader and IsEmptyElement?
How to check if the XML elements contains any attributes with XmlTextReader
How to avoid to read the XML declaration with XmlTextReader?
How to create a XML file with XmlTextWriter?
Is it possible to add attributes with XmlTextWriter?
How to Write a Comment to a XML File with XmlTextWriter and WriteComment?
How to add the XML Declaration with XmlTextWriter and WriteStartDocument?
How to use Indentation in XmlTextWriter?
What is the usage of WriteFullEndElement and how do I use?
How to include a CData block with XmlTextWriter and WriteCData?


 
 

<< XSLT 


By Bruce McQuien
First Posted 06/28/2002
Times viewed 283

Using XML classes in .Net


Summary This article shows how to make the new .Net XML classes more intuitive and streamlined in a wrapper class.

If you are like me, you like the new .Net data access and XML classes, but are a little bewildered about how spread out they all appear to be. I have thought a little about some general principles that have helped me when writing wrapper classes, and I hope they may be helpful to you as well. These principles are a guideline only, and not to be taken dogmatically. Also, they are very basic and intuitive, but it is surprising how often they are violated. Basic Principles for Wrapper Classes: 1. The resulting class should have a smaller interface than the original class or classes. 2. The resulting interface members(properties and methods) should be more intuitive than the members of the original class/classes. 3. The wrapper class should involve fewer instantiations than the original class/classes when it is called from another application. It seems to me that if these are violated, what is the point of creating the new class in the first place. O.K., thank you for indulging me, and now on to the code. The new XML classes in .Net (XmlReader and XmlWriter) are useful because they use a streaming method for data which is lightweight and highly efficient. In this respect, it is an improvement over the DOM methodology. However, for ease of use and compactness, it is not necessarily an improvement. In order to use these new classes, you must write code like this: using System.Xml; using System.Xml.XPath; string filepath = "c:\test.xml" XmlTextReader xmlread = new XmlTextReader(); XmlTextReader xmlread = new XmlTextReader(filepath); XPathDocument xmldoc = new XPathDocument(xmlread,XmlSpace.Preserve); XPathNavigator xpathdoc = xmldoc.CreateNavigator(); XPathNodeIterator xiter = xpathdoc.Select("//Names"); xiter.MoveNext(); textBox1.Text = xiter.Current.Value; Now this code is not prohibitively complex, but it does involve 3 instantiations,5 declarations, and access to 5 interface members just to access the value of the first node. In the code below, I have created a class that I hope will simplify some of these XML interfaces, or at least give you ideas for doing your own if these are not to your liking. To do the same thing as the above code, you now have to do the following. using System.Xml; using System.Xml.XPath; using XMLAccessNS; XMLAccess xmlda = new XMLAccess(); xmlda.LoadXMLFile("c:\test.xml"); XPathNodeIterator xiter = xmlda.GetNodes("//Names"); xiter.MoveNext(); TextBox1.Text = xiter.Current.Value; As you can see, this class requires 2 instantiations, 2 declarations, and access to 4 interface members. Also, the code is easier to read and the logic is easier to follow. As you can tell from the class below, I also allow the XMLAccess object to be converted back to a DOMDocument in the ToDOMDocument() method.

Additional information


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< XSLT 





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML