none
Add namespace prefix dynamically into XML literal (VB 2008 in VS 2008) RRS feed

  • Question

  • I have one Sub which receives a String (XML namespace prefix). Then I try to insert it to XML literal, but it fails.
    Here's code:

    Public Shared Sub SaveNewDb(ByVal ns As String
     
            Dim book As XElement = _ 
            <<%= ns %>:bonus xmlns:<%= ns %>="urn:48:<%= ns %>" /> 
     
    End Sub 

    VS points out on semicolons after first <%= ns %> and says " '>' expected". As far as I know any namespace can be declared in any element. What is wrong?
    Wednesday, January 21, 2009 10:20 AM

Answers

  • I think you have to do something like this:

        Public Sub SaveNewDb(ByVal ns As String)  
            Dim nmspc As XNamespace = ns 
            Dim book As XElement = New XElement(nmspc + "book", <xyz>123</xyz>)  
            Console.WriteLine(book1)  
        End Sub  
     

    Note that in the constructor I added an Xml literal that will be appended to the created element. The only problem is that the xyz element will be in the empty namespace again (i.e. the xml will look like this - note the xmlns="" on the xyz element):

    <book xmlns="xxx">  
      <xyz xmlns="">123</xyz> 
    </book> 

    Probably you don't want something like this. So you probably will end up creating your xml manually like this:

        Public Sub SaveNewDb(ByVal ns As String)  
            Dim nmspc As XNamespace = ns 
            Dim book As XElement = New XElement(nmspc + "book", New XElement(nmspc + "xyz", "123"))  
            Console.WriteLine(book)  
        End Sub  
     

    Here is the result:

    <book xmlns="xxx">  
      <xyz>123</xyz> 
    </book> 

    Pawel
    Tuesday, March 24, 2009 12:31 AM
    Moderator
  • I think you can use embedded expressions to construct complete element or attribute names but you can't produce only part of a name using an embedded expression.
    Here is an example of a function in VB.NET that receives the prefix, local name and namespace of an element and constructs it as a VB.NET literal with embedded expressions:

        Function MakeElement(ByVal Prefix As StringByVal LocalName As StringByVal Ns As XNamespace) 
            Return <<%= Ns.GetName(LocalName) %> <%= XNamespace.Xmlns.GetName(Prefix) %>=<%= Ns %>></> 
        End Function 

    When you use it as follows:

            Dim bonus As XElement = MakeElement("pf1""bonus""http://example.com/2009/ns1"
            Console.WriteLine(bonus) 
            bonus = MakeElement("p""bonus""urn:48:" + "p"
            Console.WriteLine(bonus) 

    the output of those two constructed elements is as follows:

    <pf1:bonus xmlns:pf1="http://example.com/2009/ns1"></pf1:bonus> 
    <p:bonus xmlns:p="urn:48:p"></p:bonus> 

    I hope that helps. If not then please show a sample of the XML you want to construct and which input parameters you want to pass to your function or subroutine.




    MVP XML
    • Marked as answer by JohnyL Tuesday, October 13, 2009 4:07 AM
    Tuesday, March 24, 2009 2:06 PM

All replies

  • I think you have to do something like this:

        Public Sub SaveNewDb(ByVal ns As String)  
            Dim nmspc As XNamespace = ns 
            Dim book As XElement = New XElement(nmspc + "book", <xyz>123</xyz>)  
            Console.WriteLine(book1)  
        End Sub  
     

    Note that in the constructor I added an Xml literal that will be appended to the created element. The only problem is that the xyz element will be in the empty namespace again (i.e. the xml will look like this - note the xmlns="" on the xyz element):

    <book xmlns="xxx">  
      <xyz xmlns="">123</xyz> 
    </book> 

    Probably you don't want something like this. So you probably will end up creating your xml manually like this:

        Public Sub SaveNewDb(ByVal ns As String)  
            Dim nmspc As XNamespace = ns 
            Dim book As XElement = New XElement(nmspc + "book", New XElement(nmspc + "xyz", "123"))  
            Console.WriteLine(book)  
        End Sub  
     

    Here is the result:

    <book xmlns="xxx">  
      <xyz>123</xyz> 
    </book> 

    Pawel
    Tuesday, March 24, 2009 12:31 AM
    Moderator
  • I think you can use embedded expressions to construct complete element or attribute names but you can't produce only part of a name using an embedded expression.
    Here is an example of a function in VB.NET that receives the prefix, local name and namespace of an element and constructs it as a VB.NET literal with embedded expressions:

        Function MakeElement(ByVal Prefix As StringByVal LocalName As StringByVal Ns As XNamespace) 
            Return <<%= Ns.GetName(LocalName) %> <%= XNamespace.Xmlns.GetName(Prefix) %>=<%= Ns %>></> 
        End Function 

    When you use it as follows:

            Dim bonus As XElement = MakeElement("pf1""bonus""http://example.com/2009/ns1"
            Console.WriteLine(bonus) 
            bonus = MakeElement("p""bonus""urn:48:" + "p"
            Console.WriteLine(bonus) 

    the output of those two constructed elements is as follows:

    <pf1:bonus xmlns:pf1="http://example.com/2009/ns1"></pf1:bonus> 
    <p:bonus xmlns:p="urn:48:p"></p:bonus> 

    I hope that helps. If not then please show a sample of the XML you want to construct and which input parameters you want to pass to your function or subroutine.




    MVP XML
    • Marked as answer by JohnyL Tuesday, October 13, 2009 4:07 AM
    Tuesday, March 24, 2009 2:06 PM