C#XmlDocument.CreateDocumentType
我试图找出CreateDocumentType()在C#中的工作原理,尽管我已经找到并阅读了它的msdn页面,但我无法让它为我工作 我只是想在xml文档中创建以下行:C#XmlDocument.CreateDocumentType,c#,xmldocument,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"> 这不起作用。不确定您到目前为止尝试了什么,但该方法的msdn页面(Cre
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
这不起作用。不确定您到目前为止尝试了什么,但该方法的msdn页面(CreateDocumentType,然后将子对象添加到文档)中的现有示例似乎工作正常,只需传递参数“html”、“-//W3C//DTDxHTML1.0 Transitional//EN”和“获取您提到的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的基本目录中得到一个DirectoryNotFoundException,该目录引用-//W3C//DTD HTML4.01//EN。如果继续执行此代码并在下载dtd时等待,则可能会收到一条XmlException消息:“--”是意外的令牌。所需的令牌为“>”。第81行,位置5。您可以继续执行此代码,xml文档将按预期输出
您可以将此代码包装在try-catch块中,等待它安静地抛出以前遇到的异常并继续此文档,也可以将XmlResolver属性设置为null,文档将不会尝试解析doctype
要解决原来的问题:
调用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
祝你好运 你能把你目前掌握的密码发出去吗?
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();
}
}
}