Change Xml Declaration, or select xml without declaration section

.net, c#, xml, xpath

Solution

Are you trying to change the XML programmatically? If so you can do so by creating new `XmlDeclaration` and replacing it with the previous one as shown below:

XmlDeclaration xmlDeclaration;
xmlDeclaration = doc.CreateXmlDeclaration("1.0", "iso-8859-1", null);
doc.ReplaceChild(xmlDeclaration, doc.FirstChild);

You just have to make sure that the first child of the document is Xml Declaration.

Problem

I need to change the xml declaration section of by document, or just select the data minus the declaration. Which is easier? This is an example of what my xml looks like: ``` <?xml version="1.0" encoding="utf-16"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master page-height="11in" page-width="8.5in" margin-top="0.50in" margin-left="0.8in" margin-right="0.8in" margin-bottom="0.25in" master-name="PageMaster"> <fo:region-body border-style="none" border-width="thin" margin-top="0in" margin-left="0in" margin-right="0in" margin-bottom="0.25in"/> <fo:region-after border-style="none" border-width="thin" extent="0.25in"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="PageMaster"/> </fo:root> ``` I'm trying to change the xml declaration to be: ``` <?xml version="1.0" encoding="iso-8859-1"?> ```

Original source