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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top