C# XmlDocument.CreateDocumentType

标签 c# xmldocument

我试图找出 CreateDocumentType() 在 C# 中的工作原理,虽然我已经找到并阅读了它的 msdn 页面,但我无法让它为我工作。

我只是想在我的 xml 文档中创建这一行:

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

谁能帮我解决这个问题所需的语法

编辑:到目前为止的代码,其中 htmldoc 是在代码中进一步声明的 xmldocument。

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);

这行不通。

最佳答案

问候,

首先让我们看一个简单的例子:

        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);

如果您在开发人员 IDE(Visual Studio、MonoDevelop、SharpDevelop)中运行此代码,您可能会在 AppDomain 的基本目录中收到引用 -//W3C//DTD HTML 4.01//EN 的 DirectoryNotFoundException。如果您继续执行此代码并等待下载 dtd,您可能会收到一个带有消息的 XmlException:'--' 是一个意外的标记。预期的标记是“>”。第 81 行,位置 5。您可以继续执行这段代码,xml 文档将按预期输出。

您可以将此代码包装在一个 try catch block 中,等待它安静地抛出前面提到的异常并继续处理此文档,或者您可以将 XmlResolver 属性设置为 null,这样文档将不会尝试解析文档类型.

解决原始问题:

CreateDocumentType 调用中的参数不正确。您不需要指定 PUBLIC 并且应该交换 dtdLink 和 dtdDef。这是对创建正确文档类型节点的原始帖子的修订。

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();
            }
        }
    }

请注意,此代码适用于 MS Windows 和 Linux 的 Mono 祝你好运。

关于C# XmlDocument.CreateDocumentType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3052849/

相关文章:

c# - 使用 LinqToXml 插入 XElement 以围绕另一个 XElement

.net - XMLDocument dispose - 为什么它不支持 IDisposable?

php - XML 和 PHP 空白的诅咒

c# - 如何检查 XML 节点是否存在于另一个节点的子节点中?

c# - 在 Windows 应用商店应用程序中编辑 XML 文件

c# - 使用 XmlNamespaceManager 将命名空间添加到 XmlDocument

c# - 如何将 System.Net.ICredentials 传递给 API?

c# - 如何在C#中设计集合

c# - 使用 VC++ MFC 对话从数据库中检索数据

c# - SQL 服务器错误 : ExecuteNonQuery: Connection property has not been initialized