login about faq

When I use xml control, I always get the following line at the head of the xml

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

Most applications are not effected by this, but some critical apps hiccup on this..

Any way to remove this via the xml dll w/o me having to do this within my code?

asked May 07 '14 at 11:21

chilkat's gravatar image

chilkat ♦♦

edited May 07 '14 at 11:22

The GetXml method will include the XML declaration when generated from the root node of the document. To suppress it, make a dummy node the root. The dummy node would contain one child, which is the actual root node of the document. You can then GetXml() from the child instead.

For example:

Chilkat.Xml xmlDummy = new Chilkat.Xml();
Chilkat.Xml xmlRoot = xmlDummy.NewChild("someTag","");

Calling xmlDummy.GetXml() produces a result that includes the XML declaration. Calling xmlRoot.GetXml() will not include the XML declaration in the result.


answered May 07 '14 at 11:31

chilkat's gravatar image

chilkat ♦♦

Setting the property put_EmitXmlDecl() ought to suppress the xml declaration in GetXml() and SaveXml() according to the docs, but that has no effect. If calling GetXml() on the root element always includes the declaration, then I would suggest removing put_EmitXmlDecl() and get_EmitXmlDecl().

(Feb 26 '15 at 05:33) roan98dk

I have run into a problem in this relation.

Using the NewChild() approach and calling GetXml() on the child object emits the the XML declaration. So far so good.

The problem is that the DOCTYPE (if specified) also gets emitted. This happens no matter if I apply the DOCTYPE to the parent element (the one called xmlDummy above) or the child element (the one called xmlRoot above).

I see no reason why the DOCTYPE should be emitted as well, but I guess this is because I call GetXml() on a child element and DOCTYPE is only added when calling GetXml() on the parent element.

P.S. A minor cosmetic issue is that emitting the XML declaration does not emit the CrLf after the declaration.


answered Mar 02 '15 at 10:10

roan98dk's gravatar image


I'm not able to reproduce this behavior. What programming language, operating system, etc. do you use? I'll provide a pre-release of v9.5.0.48 for you to test. If the issue still exists, tell me exactly how to reproduce it.

(Mar 03 '15 at 12:45) chilkat ♦♦

Sorry, I should have provided more detail. I only tested with the libs v9.5.0.47 for VC++ 12.0 (v120_xp) in Windows 8.1.

(Mar 03 '15 at 13:04) roan98dk

I did the same test with the latest nuget package for .Net / x86. Here is some same code:

var xml = new Chilkat.Xml();
var xmlRoot = xml.NewChild("html", "");
xmlRoot.DocType = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
xmlRoot.NewChild2("body", "");

// Incl. Xml declaration
xml.GetXml() returns "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n<unnamed>\r\n<html>\r\n        <body />\r\n</html>\r\n</unnamed>\r\n"

// Emit Xml declaration
xmlRoot.GetXml() returns "\r\n<html>\r\n<body />\r\n</html>\r\n"

As you can see the Xml declaration as well as the DOCTYPE gets emitted when I call xmlRoot.GetXml(). I ran some other tests where DOCTYPE is added using xml.DocType (above) and where I attempt set xml.EmitXmlDecl = true. In all the cases the result is exactly the same.

Now that I come to think about it, I regret my previous comment about removing EmitXmlDecl as an option. In my opinion DOCTYPE should only be possible on the top-most element i.e. xml.DocType instead of xmlRoot.DocType above. The problem with this is that then I would have to call GetXml() as xml.GetXml() which will include the Xml declaration. This however I believe to be a bad default behavior also because I may want to have an xml declaration added while calling GetXml() on a child element.

In other words I would suggest to let EmitXmlDecl fully control the behavior of the Xml declaration. Default behavior should be to add an Xml declaration to GetXml() and then the user can specify EmitXmlDecl to emit the xml declaration whether GetXml() is called on the top-most element or a child.


answered Mar 03 '15 at 15:52

roan98dk's gravatar image


Your example is confusing because "xml" is actually the root node, and "xmlRoot" is a child of "xml". Everything in your example above is working as it should. xml.GetXml() returns the full XML with declarator and DOCTYPE because it is the root, and xmlRoot.GetXml() does not because it is NOT actually the root.

(Mar 03 '15 at 19:21) chilkat ♦♦

Sorry for that. I know my example is not the best. It was only because you did the same in your example above. In any case the problem is that I want to be able to call GetXml() and get DOCTYPE included but not the declaration. This does not seem to be possible, but in my humble opinion it should be. I don't want to make a big deal out of it, since I could easily trim the declaration myself after calling GetXml(). I just feel the default behavior should be changed slightly.

(Mar 04 '15 at 03:43) roan98dk

Now that I come to think about it, my suggestion should be slightly altered in order to not impact current implementations.

  1. Setting EmitXmlDecl = true on the top element (var xml in example) should emit the declaration when calling xml.GetXml(), but keep the DOCTYPE if specified.

  2. Add a new property PrependXmlDecl (or similar name) that if set to true on a child element, should insert an xml declaration when calling GetXml(). In the above example that would be xmlRoot.GetXml(). Alternately keep the EmitXmlDecl, but let it default to true on children.

Would this be possible?

(Mar 11 '15 at 05:18) roan98dk
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: May 07 '14 at 11:21

Seen: 5,415 times

Last updated: Mar 11 '15 at 05:18

powered by OSQA