c# how to create XML without XML tag using XMLDocument and XMLWriter

[sam_ad id=”48″ codes=”true”]Hi Guys,

I had this problem where I just wanted to create a XML file without a XML tag.

Frankly I was just creating a HTML snippet which I needed to embed in some other file.

I am not going to show you all that code but instead I will show you the important bits.

Below is the code that you should be looking for to create a XML file without XML tag.

So we are using XMLDocument class to create a XML document.

I am just using file name to include .html because that fit my scenario.

Feel free to change it to any other extension.

[geshi lang=”csharp” nums=”1″ target=”_self” ]

Above code will produce the output file with contents below

Important thing in the above code is utilizing XmlWriterSettings class and putting OmitXmlDeclaration property to false. Make sure that you also set ConformanceLevel to Fragment, by default it is set to Document and when XMLWriter is outputting a document it always will include XML tag.

Lets omit XmlWriterSettings from the above code to make it equivalent to the code below
[geshi lang=”csharp” nums=”1″ target=”_self” ]


Above code will now output file called somefile.html with following contents

I hope this helps

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.