How to add a !DOCTYPE to an XmlDocument

I want to create an XmlDocument using the framework.
It has to have a <!DOCTYPE xxx  yyy "/hshs/shsh.dtd"> line in the XML.

This seems to be represented by the DocumentType property of  XmlDocument.

I cannot see how to add a DOCTYPE to a new document.

Thanks
Mike
mikexxxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dlan75Commented:
Hi,
Just at the top of your document after the <?xml tag add the doctype description as the exaple below :

<?xml version="1.0"?>
<!DOCTYPE Hello [
  <!ELEMENT Hello (#PCDATA)>
]>
<Hello>HelloJupiter!</Hello>

Hope that helped
0
dlan75Commented:
Hi,
Sorry I forgot that is if your DTD is internal for an external one it has to be like this :

<?xml version="1.0"?>
<!DOCTYPE EMail SYSTEM "http://src.selfhtml.org/email.dtd">
0
Geert BormansInformation ArchitectCommented:
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Geert BormansInformation ArchitectCommented:
 //Create a document type node and  
    //add it to the document.
    XmlDocumentType doctype;
    doctype = doc.CreateDocumentType("book", "http://mydtd.com/book.dtd", "book.dtd", null);
    doc.AppendChild(doctype);

will render something like
<!DOCTYPE book PUBLIC "http://mydtd.com/book.dtd" "book.dtd" >
0
erikTsomikSystem Architect, CF programmer Commented:
0
mikexxxAuthor Commented:
Thanks for your quick response.

I can now add a DOCTYPE to a new document.

Out of interest, does anyone know how to add one to an existing document. I cannot get that to work!

Thanks for your help.
Mike
0
Geert BormansInformation ArchitectCommented:
what method do you use?
using the CreateDocumentType method, this should work the same
doc.AppendChild(doctype);
make sure that doc. is the document node, not one of the children

can you post the relevant part of your code?
0
Geert BormansInformation ArchitectCommented:
sorry, I just got this from the method description

>The DocumentType node must also be inserted before the root element of the XmlDocument (if the document already has a root element, you cannot add a DocumentType node).

This means that adding a doctype later will not work.
Simply create a new document, add the doctype and copy the root node and all its children from the 1st document to the new document

sorry for the misguidance

cheers
Geert
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikexxxAuthor Commented:
Thanks for your help. I am making progress with my project.
I am now using XmlTextWriter.WriteDocType() method to add the DOCTYPE.
How can I add the <?xml version="1.0"?> before the DOCTYPE?

If I add it as a String using WriteString(), it will not add the DOCTYPE?

Thanks
Mike
0
mikexxxAuthor Commented:
Thanks for your help. I don't know why I couldn't find the answer myself. Sometimes you just need someone else's help.
Thanks
Mike
0
Geert BormansInformation ArchitectCommented:
do a WriteStartDocument() before you do something else

    // Write xml declaration
    xmlWriter.WriteStartDocument();

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.