C # LINQ TO XML - remove "[]" characters from DTD header

I recently created a small C # windows / LINQ to XML application in VS2010 that does exactly what it should do, with one exception: it appends โ€œ[]โ€ to the end of the DOCTYPE tag, which apparently causes files that must be diverted from the legacy system. Here a before and after:

Before

<!DOCTYPE ichicsr SYSTEM "http://www.accessdata.fda.gov/xml/icsr-xml-v2.1.dtd"> 

After

 <!DOCTYPE ichicsr SYSTEM "http://www.accessdata.fda.gov/xml/icsr-xml-v2.1.dtd"[]> 

These characters are added after saving the file in the program using the .Save function. The program allows you to select an XML file, then "cleans" it, removing certain tags, and then saves it. When the process begins, the files do not have "[]" in DOCTYPE. After saving, they do it. Does LINQ to XML Add These?

Is there a way to keep the program from adding these characters?

+8
c # xml visual-studio-2010 linq-to-xml
source share
2 answers

Obviously, when an XDocument parses an XML document containing a document type declaration, an empty "inner subset" is automatically inserted if it does not exist. (The inner subset is the part surrounded by [] in <!DOCTYPE> ).

The result is a well-formed XML. However, if your legacy system cannot process it, you can remove the internal subset from the DTD by setting the XDocumentType.InternalSubset property to null :

 XDocument document = ...; if (document.DocumentType != null) document.DocumentType.InternalSubset = null; 
+11
source share

If you are dealing with this in Mono (e.g. cod3monk3y) for cases such as modifying Info.plist, you can use the old XmlDocument class to correct the situation after using XDocument to create / modify your xml file.

The code assumes that your file "Info.plist" is in the infoPlist path:

 using System; using System.IO; using System.Linq; using System.Xml; using System.Xml.Linq; var xDocument = XDocument.Load (infoPlist); // Do your manipulations here xDocument.Save (infoPlist); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load (infoPlist); if (xmlDocument.DocumentType != null) { var name = xmlDocument.DocumentType.Name; var publicId = xmlDocument.DocumentType.PublicId; var systemId = xmlDocument.DocumentType.SystemId; var parent = xmlDocument.DocumentType.ParentNode; var documentTypeWithNullInternalSubset = xmlDocument.CreateDocumentType(name, publicId, systemId, null); parent.ReplaceChild(documentTypeWithNullInternalSubset, xmlDocument.DocumentType); } xmlDocument.Save (infoPlist); 
+7
source share

All Articles