Click to See Complete Forum and Search --> : Update XML


venutm
March 6th, 2006, 03:52 PM
I want to remove encoding="UTF-8" from the line: <?xml version="1.0" encoding="UTF-8"?> in an xml file using C#.

I have seen fuctions to update other elelments but not this, can anyone please help....

I want to update an existing xml file with element: <?xml version="1.0" encoding="UTF-8"?> to <?xml version="1.0"?>.

Thanks!

wildfrog
March 6th, 2006, 04:43 PM
Maybe this will do the trick:

// Load document.
XmlDocument pDoc = new XmlDocument();
pDoc.Load("input.xml");

// Get first node (and check if it is a xml declaration).
XmlDeclaration pDecl = pDoc.FirstChild as XmlDeclaration;
if (pDecl == null)
{
// No xml declaration exists, create one.
pDecl = pDoc.CreateXmlDeclaration("1.0", null, null);
pDoc.InsertBefore(pDecl, pDoc.FirstChild);
}
else
{
// Modify the one we got.
pDecl.Encoding = null;
}

// Save document.
pDoc.Save("output.xml");

- petter

venutm
March 7th, 2006, 09:06 AM
May you win a million dollar lottery!
Thanks.