The Mighty Blog

Dec7

Written by:Will
12/7/2007  RssIcon

For some odd reason, the XPathDocument class does not support loading the object using a string. However, this seems to be an integral requirement for some applications where the XML is generated and parsed on-the-fly. Here's how to do that...

For some odd reason, the XPathDocument class does not support loading the object using a string.  However, this seems to be an integral requirement for some applications where the XML is generated and parsed on-the-fly.  Here's how to do that...

Dim byteObj() As Byte
byteObj = System.Text.Encoding.UTF8.GetBytes(SerializedObject)
Dim memStr As System.IO.MemoryStream = New System.IO.MemoryStream(byteObj)
Dim xpathDoc As XPathDocument = New XPathDocument(memStr)

That is only four lines of code.  Hey!  That's pretty good...  But what if you didn't want to use four lines?  Can't we bring this down to less lines of code?  Yes!  Here is the same example in only two lines of code, and less memory overhead, because there are also less objects.

Dim memStr As System.IO.MemoryStream = New System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(SerializedObject))
Dim xpathDoc As XPathDocument = New XPathDocument(memStr)

What if you wanted to globalize this to make it even easier?  Couldn't you add this as a shared/static function to an existing class?  Sure, but it could get even better... 

You can take that a step further by extending the XPathDocument class, for use in your entire application.  This way, you have then limited those two lines of code to one - each time you do it.  Don't forget that your footprint is also smaller since you are extending an existing class.

Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.XPath
Namespace MyCompany.Extensions
    Public Class XPathDocument         Inherits System.Xml.XPath.XPathDocument
        Public Sub New(ByVal xmlString As String)             MyBase.New(New MemoryStream(Encoding.UTF8.GetBytes(xmlString)))         End Sub
    End Class
End Namespace

Now, you can call this from anywhere in your code like so:

Dim strXml As String = "<rootNode><element1>somevalue</element1></rootNode>"
Dim xpathDoc As New MyCompany.Extensions.XPathDocument(strXml)

Now, we have reduced this to one line of code! :) Have fun!


Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Add to Technorati Favorites
Tweet about my blog
Will Strohl - The Mighty Blog - RSS Feed

Tag Cloud

Sort by:Tag | SizeRSS
camp   community   dnn   dotnetnuke   dotnetnuke®   event   example   free   get   group   integer   jquery   meeting   module   new   odug   orlando   search   session   think  
The opinions expressed here are the personal opinions of Will Strohl and do not necessarily represent the views and opinions of the DotNetNuke Corporation.
© Copyright 2004-2010 by Will Strohl. All rights reserved.Website Skinned By: Ralph Williams  Website Hosted By: Applied Innovations