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

5 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

kbAlertz.com :: Visual Studio 2005

kbAlertz.com :: Visual Studio 2008

kbAlertz.com :: Visual Basic 2005