Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

September 2, 2010

XML Literals: Creating XML with a Variable Namespace

Filed under: VB.NET,XML @ 12:24 am

This post covers the technique for creating XML that includes an XML namespace (xmlns) attribute, when that attribute is not predefined at compile time.

This post continues the series on using XML literals which starts here. If you need to create XML with a namespace that is predefined at compile time, you can use the simple technique detailed in this prior post. Since XML literals are a VB.NET feature only (new in VB 9/VS 2008),  this post only includes VB.NET code.

The scenario is the same as the prior post, except that the XML contains an XML namespace attribute that is not known at runtime. Here is the text from the  prior post:

The scenario is that we need to build an XML document, maybe to send to a Web service or to save data to a local file system. In this case, the task is to write customer information, including the customer’s name.

Here is the XML that we want to create:

<Customers xmlns="http://customer/2010/info">

But the issue is that our requirement states that the namespace (xmlns) can change at runtime. Maybe different Web service vendors need different namespaces or maybe there is a test and production version of the namespace. In any case, you can’t "hard-code" the namespace into the XML literals.

So the trick in this case is to use a "fake" namespace in the XML literals. Then replace the "fake" one with the real one at runtime.

In VB:

Private Function BuildXML(ByVal lastName As String,
                          ByVal firstName As String) As String

    Dim xmlDoc = <Customers xmlns="">
                         <LastName><%= lastName %></LastName>
                         <FirstName><%= firstName %></FirstName>
    Dim realNamespace As String = "

    xmlDoc = XElement.Parse(xmlDoc.ToString.
               Replace("", realNamespace))

    Return xmlDoc.ToString
End Function

Notice the fake namespace (xmlns) attribute. It can contain any namespace name. Using a fake namespace allows you to use XML literals even if you don’t know the namespace at compile time.

Then use the String class Replace method to replace the "fake" namespace with the real one. Working from the inside out, this code takes the XML, converts it to a string, replaces the namespace, then uses the XElement Parse method to convert the string back to XML.

This example still hard-codes the correct namespace into the realNamespace variable, but since it is a variable the value could come from anywhere. It could be read from a table or from a configuration file or be passed in as a command line parameter. (This last part is not really necessary if you plan to pass the result back out of the function as a string as shown in this example.)

Use this technique any time you need to create XML that contains a namespace and you cannot hard-code that namespace into the XML literals.


[Thanks to Joacim Andersson for the code to accomplish this task!]

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2021 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs