tags:

views:

29

answers:

2

Hi, I am transforming an XML document but after the transform my DTD goes away and also the first line which tells the XML version is missing.

<?xml version="1.0"?>

The code I am using to transform the XML file is:

// Load the style sheet.
            var xslt = new XslCompiledTransform();
            xslt.Load("XSLTFile1.xslt");

            // Create the writer.
            var settings = new XmlWriterSettings
                            {
                                Indent = true,
                                IndentChars = "\t",
                                ConformanceLevel = ConformanceLevel.Auto,
                                Encoding = Encoding.UTF8,
                            };

            var mydoc = XDocument.Load("Doc1.xml"); 

            var writer = XmlWriter.Create("Doc2.xml", settings);

            // Execute the transform and output the results to a file.
            if (writer != null)
            {
                xslt.Transform(mydoc.CreateReader(), writer);
                writer.Close();
            }

Any ideas?

A: 

In order to keep the XML declaration, you need to make sure in your XmlWriterSettings that OmitXmlDeclaration is set to false:

var settings = new XmlWriterSettings
                   {
                     Indent = true,
                     IndentChars = "\t",
                     ConformanceLevel = ConformanceLevel.Auto,
                     Encoding = Encoding.UTF8,
                     OmitXmlDeclaration = false,
                    };

As for the DTD "going away" - since you are transforming the document, you should add a new DTD declaration to the transformed document.

Without the xsl and xml files, it is difficult to tell for certain. Can you edit your question and add them?

Oded
A: 

hey thanks for the reply. I did

writer.WriteDocType(mydoc.DocumentType.Name, mydoc.DocumentType.PublicId, mydoc.DocumentType.SystemId, mydoc.DocumentType.InternalSubset); 

And it worked. Yes I also have

OmitXmlDeclaration = false,

Thanks

LibraRocks