Search code examples
xmlvb.netxml-namespacesxmldocumentsystem.xml

Create an XML element with namespace in XMLDocument


I'm working with the XMLDocument class in VB.net.
I want to get element with name "a:Product", but following code:

dim doc as new XMLDocument 
dim pr as XMLElement = doc.CreateElement("a:product")
pr.InnerText = "123"
doc.AppendChild(pr)

return element in XML without "a:":

<product>123</product>

Where do I go wrong?


Solution

  • Use this instead:

    dim pr as XMLElement = doc.CreateElement("a:product", namespaceURI);
    

    Where namespaceURI is the namespace represented by the alias "a". This is usually something like "http://schema.example.com/blahblah". Just look it up in your XML file.