Windows Phone Developers

Friday, January 2, 2009

How to add XML Declaration to an XML document using C# (.NET)

Programmatically insert XML Declaration using C# (.NET)

The following snippet will be useful for inserting the XML Declaration for a given XML file.

private static void Add_Declaration_2_XML(String sXMLFile)

{

try

{

XmlDocument XDoc = new XmlDocument();

XDoc.Load( sXMLFile);

XmlDeclaration XDec;

XDec = XDoc.CreateXmlDeclaration("1.0", "us-ascii", "yes");

XmlElement XElem = XDoc.DocumentElement;

XDoc.InsertBefore(XDec, XElem);

XDoc.Save(sXMLFile + ".new");

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

XMLFile before Insertion

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment