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

后端 未结 2 1811
别跟我提以往
别跟我提以往 2020-12-06 02:30

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 DOC

相关标签:
2条回答
  • 2020-12-06 02:55

    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;
    
    0 讨论(0)
  • 2020-12-06 03:19

    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);
    
    0 讨论(0)
提交回复
热议问题