开发者

C# XmlDocument.CreateDocumentType

I am trying to figure out of the CreateDocumentType开发者_如何转开发() works in C# and although i have already found and read the msdn page on it, i can not get it to work for me.

I am simply trying to create this line in my xml document:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Can someone help me out with the syntax required for this

EDIT: code so far, with htmldoc being an xmldocument declared further up in the code.

string dtdLink = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
string dtdDef = "-//W3C//DTD XHTML 1.0 Transitional//EN";

XmlDocumentType docType = htmlDoc.CreateDocumentType("html", "PUBLIC", dtdLink, dtdDef);
htmlDoc.AppendChild(docType);

This does NOT work.


Greetings,

First let's examine a simple example:

        XmlDocument document = new XmlDocument();
        XmlDocumentType doctype = document.CreateDocumentType("html", "-//W3C//DTD HTML 4.01//EN", "http://www.w3.org/TR/html4/strict.dtd", null);
        document.AppendChild(doctype);

If you run this code within an developer IDE (Visual Studio,MonoDevelop,SharpDevelop) you will probably get an DirectoryNotFoundException refering to -//W3C//DTD HTML 4.01//EN at the base directory of your AppDomain. If you continue the execution of this code and wait while the dtd is downloaded, you will probably get an XmlException with an message: '--' is an unexpected token. The expected token is '>'. Line 81, position 5. You can continue the execution of this code and the xml document will output as expected.

You could wrap this code in a try catch block and wait for it to quietly throw the previously metioned exceptions and continue with this document, or you could set the XmlResolver property to null, and the document will not try to resolve the doctype.

To address the original question:

The parameters in the call to CreateDocumentType are not correct. Your don't need to specify PUBLIC and the dtdLink and dtdDef should be swapped. Here is a revision to the original posting that creates the correct document type node.

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string dtdLink = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
                string dtdDef = "-//W3C//DTD XHTML 1.0 Transitional//EN";

                // Create an xml document
                XmlDocument htmlDoc = new XmlDocument();
                /// Set the XmlResolver property to null to prevent the docType below from throwing exceptions
                htmlDoc.XmlResolver = null;

                try
                {
                    // Create the doc type and append it to this document.
                    XmlDocumentType docType = htmlDoc.CreateDocumentType("html", dtdDef, dtdLink, null);
                    htmlDoc.AppendChild(docType);

                    // Write the root node in the xhtml namespace.
                    using (XmlWriter writer = htmlDoc.CreateNavigator().AppendChild())
                    {
                        writer.WriteStartElement("html", "http://www.w3.org/1999/xhtml");
                        // Continue the document if you'd like.
                        writer.WriteEndElement();
                    }
                }
                catch { }

                // Display the document on the console out
                htmlDoc.Save(Console.Out);

                Console.WriteLine();
                Console.WriteLine("Press Any Key to exit");
                Console.ReadKey();
            }
        }
    }

Note that this code works in MS Windows and Linux with Mono Good luck.


Not sure what you've tried so far, but the existing sample in the method's msdn page (CreateDocumentType, then AppendChild to the doc) seems to work fine, just passing params of "html", "-//W3C//DTD XHTML 1.0 Transitional//EN", and "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" to get the doctype you mentioned

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜