xmlvb.net

How to create nested XML nodes in vb.net


I try to create a node with help of a xElement. The node has namespaces namely 'rql' and 'rdf'.

I have problems to create the node because of the namespaces.

The result should look like this:

<rdf:_11 rdf:parseType="Resource">
    <rql:object>R1</rql:object>
    <rql:field>
        <rdf:Seq>
            <rdf:li rdf:resource="http://com.ibm.rdm/navigation#tag"/>
            <rdf:li rdf:resource="http://purl.org/dc/terms/title"/>
        </rdf:Seq>
    </rql:field>
</rdf:_11>

I tried already to use namespaces in this way:

Dim rdf As XNamespace = MyKeyContainer.MyNamespace("rdf")
Dim MyNode As New XElement(rdf + "_11")

but I'm not able to create the node like in the result.


Solution

  • You should use XElement and XNamespace to make a nested Structure with namespaces included.

    So it should look something like this (Old, Scroll Down a bit for Edit 2):

    Imports System.Xml.Linq
    
    Module Module1
        Sub Main()
            ' Define the namespaces
            Dim rdf As XNamespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            Dim rql As XNamespace = "http://example.com/rql"
    
            ' this will create the XML structure
            Dim xml As New XElement(rdf + "_11",
                New XAttribute(rdf + "parseType", "Resource"),
                New XElement(rql + "object", "R1"),
                New XElement(rql + "field",
                    New XElement(rdf + "Seq",
                        New XElement(rdf + "li",
                            New XAttribute(rdf + "resource", "http://com.ibm.rdm/navigation#tag")
                        ),
                        New XElement(rdf + "li",
                            New XAttribute(rdf + "resource", "http://purl.org/dc/terms/title")
                        )
                    )
                )
            )
    
            ' as an example it will output the XML to console or wherever you needed for
            Console.WriteLine(xml)
        End Sub
    End Module
    

    Edit 2:

    Imports System.Xml.Linq
    
    Module Module1
        Sub Main()
            ' Define the namespaces
            Dim rdf As XNamespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            Dim rql As XNamespace = "http://example.com/rql"
    
            ' this will create the XML structure
            Dim xml As New XElement(rdf + "_11",
    
                New XAttribute(XNamespace.Xmlns + "rdf", rdf),
                New XAttribute(XNamespace.Xmlns + "rql", rql),
    
                New XAttribute(rdf + "parseType", "Resource"),
                New XElement(rql + "object", "R1"),
                New XElement(rql + "field",
                    New XElement(rdf + "Seq",
                        New XElement(rdf + "li",
                            New XAttribute(rdf + "resource", "http://com.ibm.rdm/navigation#tag")
                        ),
                        New XElement(rdf + "li",
                            New XAttribute(rdf + "resource", "http://purl.org/dc/terms/title")
                        )
                    )
                )
            )
    
            ' as an example it will output the XML to console or wherever you needed for
            Console.WriteLine(xml)
        End Sub
    End Module
    

    The output:

    <rdf:_11 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rql="http://example.com/rql" rdf:parseType="Resource">
      <rql:object>R1</rql:object>
      <rql:field>
        <rdf:Seq>
          <rdf:li rdf:resource="http://com.ibm.rdm/navigation#tag" />
          <rdf:li rdf:resource="http://purl.org/dc/terms/title" />
        </rdf:Seq>
      </rql:field>
    </rdf:_11>