Windows Phone Developers

Tuesday, December 16, 2008

Remove DOCTYPE declaration from XML Files using C#

How to remove Document Type Definition (DTD) declarations from XML Files using C#

XMLSchema is fast replacing DTD. If you have old XML files that link to DTD, you might want to remove the Doctype declarations. The following code should help you in that

private static void Remove_DTD_From_XML(String sXMLFile)

{

try

{

XmlDocument XDoc = new XmlDocument();

XDoc.Load(sXMLFile);

XmlDocumentType XDType = XDoc.DocumentType;

XDoc.RemoveChild(XDType);

XDoc.Save(sXMLFile + ".changed");

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

6 comments:

  1. But it does not resolve the entities in the XML document.

    ReplyDelete
  2. If the DTD not exists, Error is occured on load document. The same problem always.

    ReplyDelete
  3. To Anonymous, here is the VB.NET code that I use to ignore the DTD

    ' do not resolve DTD
    Dim settings As XmlReaderSettings = New XmlReaderSettings()
    settings.XmlResolver = Nothing

    ' read xml file
    Dim str As XmlReader = XmlTextReader.Create(fileName, settings)
    Dim doc As XmlDocument = New XmlDocument()
    doc.Load(str)

    ' remove DOCTYPE tag
    If doc.DocumentType IsNot Nothing Then
    Dim XDType As XmlDocumentType = doc.DocumentType
    doc.RemoveChild(XDType)
    End If

    ReplyDelete
  4. It's pretty helpful,i should recommended it to my freinds.
    thanks a lot

    ReplyDelete
  5. solved my issue..thanks

    ReplyDelete
  6. At COEPD we are glad to announce Web Designing Internship Programs (Self sponsored) for professionals who want to have hands on experience. Associations with IT Companies, at present we are providing this program in COEPD Hyderabad premises. We anticipate finest web designing technology covering Photoshop, HTML, CSS, HTML5, CSS3, JavaScript, Boot Strap, and JQuery. We tutor participants to be solution providers and creative engineers. This internship is intelligently dedicated to our enthusiastic and passionate participants mostly acknowledging and appreciating the fact that they are on the path of making a career in Web Designing. This internship is designed to fortify that in addition to gaining the vital theoretical knowledge, the readers gain sufficient hands-on practice and practical know-how to master the essence of Web Designing profession. More than a training institute, COEPD today stands differentiated as a mission to help you "Build your dream career" - COEPD way.

    http://www.coepd.com/WebDesigning-Internships.html

    ReplyDelete