How to use XML Namespaces with XNamespace class in VB.NET

You can use the XNamespace class to create XML content that uses a namespace. You can find more information about XML namespaces from the article How to use XML Namespaces in XML document. When you want to use XNamespace class you should define an XNamespace object first. Then you should add this XNamespace object to the beginning of the element name every time you create an XElement (or an XAttribute) that you want to place in that namespace. Here’s an example:

 

Dim Ns as XNamespace = “http://www.Books.com/BooksList”

Dim Doc as XDocument = New XDocument(

New XDeclaration(“1.0”, “utf-8”, “yes”),

New XComment(“Created with the XDocument class.”),

New XElement(Ns + “BooksList”,

New XElement(Ns + “Book”,

New  XAttribute(“ISBN-13”, “978-0545139700),

New XAttribute(“Title”, “Harry Potter and the Deathly Hallows”),

New XElement(Ns+“Author”,”J.K. Rowling”),

New XElement(Ns+“Price”,”14.99),

New XElement(Ns+”Available”,”True”)

),

)

)