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?
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.
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.
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.
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.