Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I recently created a small C# windows forms/LINQ to XML app in VS2010 that does exactly what it's supposed to do, except for one thing: it adds "[]" to the end of the DOCTYPE tag, which apparently is causing files to be rejected from a legacy system. Here's 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 get added after the file is saved within the program using the .Save function. The program allows selection of an .xml file, then "cleans" it by removing certain tags, then saves it. When the process begins, the files do not have the "[]" in the DOCTYPE. After saving, they do. Does LINQ to XML add these?

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

like image 223
ewomack Avatar asked Sep 10 '12 19:09

ewomack


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

Evidently, when XDocument parses an XML document that contains a Document Type Declaration, an empty "internal subset" is automatically inserted if one doesn't exist. (The internal subset is the part surrounded by [] in the <!DOCTYPE>).

The result is well-formed XML. However, if your legacy system can't handle 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;
like image 149
Michael Liu Avatar answered Oct 18 '22 09:10

Michael Liu


If you are dealing with this on Mono (like cod3monk3y) for cases like modifying Info.plist, you can use the old XmlDocument class to fix things up after you use XDocument to create/modify your xml file.

The code assumes your "Info.plist" file is located at the path infoPlist:

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);
like image 21
aggieNick02 Avatar answered Oct 18 '22 10:10

aggieNick02