[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How to add a !DOCTYPE to an XmlDocument

Posted on 2008-02-07
11
Medium Priority
?
1,086 Views
Last Modified: 2012-06-22
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
0
Comment
Question by:mikexxx
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 12

Expert Comment

by:dlan75
ID: 20841210
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
 
LVL 12

Expert Comment

by:dlan75
ID: 20841217
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20841237
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20841280
 //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
 
LVL 19

Expert Comment

by:erikTsomik
ID: 20841353
0
 

Author Comment

by:mikexxx
ID: 20841527
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20841562
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
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 20841616
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
 

Author Comment

by:mikexxx
ID: 20841730
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
 

Author Closing Comment

by:mikexxx
ID: 31428857
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20842579
do a WriteStartDocument() before you do something else

    // Write xml declaration
    xmlWriter.WriteStartDocument();

0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question