add header

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi how to add <?xml version ="1.0" encoding="UTF-8" ?> to my xml below?
Please help.

Code:
XmlDocument xmlDom = new XmlDocument ();
xmlDom.AppendChild(xmlDom.CreateElement ("", "Request", ""));
XmlElement xmlRoot = xmlDom.DocumentElement;
XmlElement xmlBook;
XmlElement xmlTitle, xmlAuthor, xmlPrice;
XmlText xmlText;
 
How are you displaying the XML?

If you were creating an XmlWriter, for example, and you set the
OmitXmlDeclaration property to true, it would omit this on output.

If you are calling ToString on XmlDocument (or InnerXml or something
like that), then it won't be there.

My recommendation is to create an XmlWriter with a StringWriter as the
backing store, and then save the xml to that XmlWriter (making sure that
OmitXmlDeclaration is false). Then you can use that string if you want to
display it, or you can just use a different TextWriter implementation to
store the result anywhere you want.
 
Hi how to add <?xml version ="1.0" encoding="UTF-8" ?> to my xml below?
Please help.

Code:
XmlDocument xmlDom = new XmlDocument ();
xmlDom.AppendChild(xmlDom.CreateElement ("", "Request", ""));
XmlElement xmlRoot = xmlDom.DocumentElement;
XmlElement xmlBook;
XmlElement xmlTitle, xmlAuthor, xmlPrice;
XmlText xmlText;

As Nicholas said you can use the XmlWriter.

If you want to stick with using the XmlDocument, you can create the
declaration with the CreateXmlDeclaration method.

XmlDeclaration xdec = xmlDom.CreateXmlDeclaration("1.0", "UTF-8", null);
 
Back
Top