Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get XML with header (<?xml version="1.0"...)?

Consider the following simple code which creates an XML document and displays it.

XmlDocument xml = new XmlDocument();
XmlElement root = xml.CreateElement("root");
xml.AppendChild(root);
XmlComment comment = xml.CreateComment("Comment");
root.AppendChild(comment);
textBox1.Text = xml.OuterXml;

it displays, as expected:

<root><!--Comment--></root>

It doesn't, however, display the

<?xml version="1.0" encoding="UTF-8"?>   

So how can I get that as well?

like image 910
ispiro Avatar asked Feb 12 '13 18:02

ispiro


People also ask

How do you add a header in XML?

Select Topic Head from the Append Child, Insert Before, or Insert After submenus when invoking the contextual menu in the DITA Maps Manager view. Open the DITA map in the XML editor and select the Insert Topic Heading action from the main toolbar (or from the Insert submenu of the contextual menu).

What does <? XML version 1.0 ?> Mean?

version="1.0" means that this is the XML standard this file conforms to. encoding="utf-8" means that the file is encoded using the UTF-8 Unicode encoding.

How do I find the version of XML?

You can use DOMDocument to do this. Show activity on this post. These and other properties (such as standalone ) of the document are documented in the documentation, ehm, manual. $doc->version should be $doc->xmlVersion .

How do you specify XML version and encoding in an XML document?

To avoid errors, you should specify the encoding used, or save your XML files as UTF-8. UTF-8 is the default character encoding for XML documents. Character encoding can be studied in our Character Set Tutorial. UTF-8 is also the default encoding for HTML5, CSS, JavaScript, PHP, and SQL.


3 Answers

Create an XML-declaration using XmlDocument.CreateXmlDeclaration Method:

XmlNode docNode = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
xml.AppendChild(docNode);

Note: please take a look at the documentation for the method, especially for encoding parameter: there are special requirements for values of this parameter.

like image 81
Sergey Vyacheslavovich Brunov Avatar answered Sep 30 '22 14:09

Sergey Vyacheslavovich Brunov


You need to use an XmlWriter (which writes the XML declaration by default). You should note that that C# strings are UTF-16 and your XML declaration says that the document is UTF-8 encoded. That discrepancy can cause problems. Here's an example, writing to a file that gives the result you expect:

XmlDocument xml = new XmlDocument();
XmlElement root = xml.CreateElement("root");
xml.AppendChild(root);
XmlComment comment = xml.CreateComment("Comment");
root.AppendChild(comment);

XmlWriterSettings settings = new XmlWriterSettings
{
  Encoding           = Encoding.UTF8,
  ConformanceLevel   = ConformanceLevel.Document,
  OmitXmlDeclaration = false,
  CloseOutput        = true,
  Indent             = true,
  IndentChars        = "  ",
  NewLineHandling    = NewLineHandling.Replace
};

using ( StreamWriter sw = File.CreateText("output.xml") )
using ( XmlWriter writer = XmlWriter.Create(sw,settings))
{
  xml.WriteContentTo(writer);
  writer.Close() ;
}

string document = File.ReadAllText( "output.xml") ;
like image 42
Nicholas Carey Avatar answered Sep 30 '22 13:09

Nicholas Carey


XmlDeclaration xmldecl;
xmldecl = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);

XmlElement root = xmlDocument.DocumentElement;
xmlDocument.InsertBefore(xmldecl, root);
like image 44
Asif Avatar answered Sep 30 '22 13:09

Asif