1

I have some xml data that looks like..

<Root>
<Data>Nack</Data>
<Data>Nelly</Data>
</Root>

I want to add "<?xml version=\"1.0\"?>" to this string. Then preserve the xml as a string.

I attempted a few things..

This breaks and loses the original xml string

myOriginalXml="<?xml version=\"1.0\"?>"  + myOriginalXml;

This doesnt do anything, just keeps the original xml data with no declaration attached.

 XmlDocument doc = new XmlDocument();
                doc.LoadXml(myOriginalXml);
                XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8","no");
                StringWriter sw = new StringWriter();
                XmlTextWriter tx = new XmlTextWriter(sw);
                doc.WriteTo(tx);
                string xmlString = sw.ToString();

This is also not seeming to have any effect..

  XmlDocument doc = new XmlDocument();
                doc.LoadXml(myOriginalXml);
                XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "no");
                MemoryStream xmlStream = new MemoryStream();
                doc.Save(xmlStream);
                xmlStream.Flush();
                xmlStream.Position = 0;
                doc.Load(xmlStream);
                StringWriter sw = new StringWriter();
                XmlTextWriter tx = new XmlTextWriter(sw);
                doc.WriteTo(tx);
                string xmlString = sw.ToString();
Nick LaMarca
  • 8,076
  • 31
  • 93
  • 152

1 Answers1

3

Use an xmlwritersettings to achieve greater control over saving. The XmlWriter.Create accepts that setting (instead of the default contructor)

    var myOriginalXml = @"<Root>
                            <Data>Nack</Data>
                            <Data>Nelly</Data>
                          </Root>";
    var doc = new XmlDocument();
    doc.LoadXml(myOriginalXml);
    var ms = new MemoryStream();
    var tx = XmlWriter.Create(ms, 
                new XmlWriterSettings { 
                             OmitXmlDeclaration = false, 
                             ConformanceLevel= ConformanceLevel.Document,
                             Encoding = UTF8Encoding.UTF8 });
    doc.Save(tx);
    var xmlString = UTF8Encoding.UTF8.GetString(ms.ToArray());
rene
  • 41,474
  • 78
  • 114
  • 152
  • The encoding is utf-16 I need utf-8 is that an easy fix to this code? – Nick LaMarca Dec 28 '12 at 20:57
  • I added this..Encoding=System.Text.Encoding.UTF8 but the encoding stayed utf-16 – Nick LaMarca Dec 28 '12 at 21:01
  • +1. @NickLaMarca - check suggested duplicate - it contain all information you need (you need writer with encoding you want, `StringWriter` obviously is UTF-16 only). – Alexei Levenkov Dec 28 '12 at 21:02
  • This code works all the way but the UTF part. How can this code be modified to use memory stream instead of stream writer? – Nick LaMarca Dec 28 '12 at 21:06
  • @NickLaMarca I had to get rid of the StringWriter and have a memorystream instead. It now generates an xml declaration with an UTF-8 encoding in it... – rene Dec 28 '12 at 21:12