Tuesday, 11 June 2013

.Net Tips : Add XML declaration programmatically to a XML document in .Net with C#.Net and VB.Net example

XML declaration of a XML document or file is very important. We can add XML declaration and create XML element programmatically .
We are using "XDocument" and "XDeclaration" class to add declaration.

Here is example for this.
In this example we create XML document with specified XML declaration and save it into the StringWriter object. You can see in the debug time screenshot.

C#. Net Example :
        XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-16", "Yes"),new XElement("Products"));
        StringWriter sw = new StringWriter();

VB.Net Examples :
        Dim doc As New XDocument(New XDeclaration("1.0", "UTF-16", "Yes"), New XElement("Products"))
        Dim sw As New StringWriter()

Output :
Add XML declaration programmatically
(To view original image , click on image)

1 comment:

  1. nice tips...excellent stuff...regular reader

    make habit of these tips...daily