Results 1 to 4 of 4

Thread: Best way to extend XmlDocument?

  1. #1
    brendan.hill is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jun 2010
    Posts
    15
    Reputation
    38

    Best way to extend XmlDocument?

    I would like to extend the functionality of XmlDocument and XmlElement to make it easier to add new children, multiple values at once, etc, for example this function would be useful:

    Code:
    Public Function AddValues(paramarray NameThenValue() as object)
    .
    End Function
    
    Dim x as XmlElement
    .
    .
    x.AddValues("Name", "asdf", "Age", 13, "DateOfBirth", #1/1/1970#)
    What's the best way to do this? Inheriting a class from XmlDocument/XmlElement is great except the inability to cast from base class to derived class (eg. XmlElement -> XmlElementEx) means that you can't properly wrap .AppendChild() and .SelectSingleNode().

    I've since actually gone with a wrapper class (contains XmlElement as a private member, then exposes just my special functions) but it would be preferable to somehow extend the XmlElement class directly without loss of functionality.

    Any suggestions?

    -Brendan

    P.S. Not interested in 3rd party products

  2. #2
    Herman is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    448
    Reputation
    346
    Just apply the same logic you applied with the AddValues method, and create a proper constructor overload for the class. Instead of casting an object to it, pass the object through the class constructor.

    Public Class XmlDocumentEx
    Inherits XmlDocument

    Public Overloads Sub New(ByVal objXmlDocument As XmlDocument)
    Me.LoadXml(objXmlDocument.OuterXml)
    End Sub
    End Class

  3. #3
    brendan.hill is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jun 2010
    Posts
    15
    Reputation
    38
    Quote Originally Posted by Herman View Post
    Just apply the same logic you applied with the AddValues method, and create a proper constructor overload for the class. Instead of casting an object to it, pass the object through the class constructor.

    Public Class XmlDocumentEx
    Inherits XmlDocument

    Public Overloads Sub New(ByVal objXmlDocument As XmlDocument)
    Me.LoadXml(objXmlDocument.OuterXml)
    End Sub
    End Class
    Hmm the issue here is that repeated retrievals of child XmlElement nodes would result in the XML being serialized & deserialized on each request.

  4. #4
    Herman is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    448
    Reputation
    346
    How so? The XmlDocumentEx instance's .OuterXml property becomes the original objXmlDocument property, the copy is done only once.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking