Question

Ok so I have a method which should read an XML documents and then wrap them all up in a bigger XML document.

<Elements>
    <Element>
    <Element>
</Elements>

the issue is that when using the code below the output file is missing the declaration

<?xml >

any reason why this is? I am hoping it is something stupid I am missing.

XmlWriter xmlWriter = XmlWriter.Create(outputFileName, xwsSettings);

xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("Elements");

foreach (var item in items)
{
    xmlReader = XmlReader.Create(item.FullFilename);

    while (xmlReader.Read())
    {
        if(xmlReader.NodeType == XmlNodeType.Element)
        {
            xmlWriter.WriteRaw(xmlReader.ReadOuterXml());
            break;
  }
    }
}

xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();

xmlWriter.Flush();
xmlWriter.Close();
xmlWriter = null;
Was it helpful?

Solution

As @Tim had hightlighted to check I had my XmlWriterSettings incorrectly set

XmlWriterSettings xwsSettings = new XmlWriterSettings();
xwsSettings.CheckCharacters = false;
xwsSettings.CloseOutput = true;
xwsSettings.ConformanceLevel = ConformanceLevel.Document;
xwsSettings.Encoding = Encoding.UTF8;
xwsSettings.Indent = false;
xwsSettings.NewLineHandling = NewLineHandling.None;

xwsSettings.OmitXmlDeclaration = true; <---- should have been false;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top