[英]Reading and writing to an XML - DTD error
I have a program where it reads and writes XML using XMLReader and XMLWriter 我有一个程序,它使用XMLReader和XMLWriter读取和写入XML
XmlWriter writer =
XmlWriter.Create(fullpath, settings);
//content...
writer.Flush();
writer.Close();
and my reader code 和我的读者代码
XmlReader reader = XmlReader.Create(fullpath);
while (reader.Read())
{
switch(reader.NodeType)
{
case XmlNodeType.Element:
Console.WriteLine("Element: " + reader.Name);
while(reader.MoveToNextAttribute())
{
Console.WriteLine("\tAttribute: [" + reader.Name + "] = '" +
reader.Value + "'");
}
break;
case XmlNodeType.DocumentType:
Console.WriteLine("Document: " + reader.Value);
break;
case XmlNodeType.Comment:
Console.WriteLine("comment: " + reader.Value);
break;
default:
Console.WriteLine("unknown type, error!");
break;
}
}
reader.Close()
The writer works fine, but when it gets to XmlReader reader = XmlReader.Create(fullpath); 编写器工作正常,但当它到达XmlReader reader = XmlReader.Create(fullpath);
it prints the unknown type error message twice and crashes with the error 它会打印两次未知类型的错误消息并因错误而崩溃
Unhandled Exception: System.Xml.XmlException: For security reasons DTD is prohib ited in this XML document.
未处理的异常:System.Xml.XmlException:出于安全原因,在此XML文档中禁止使用DTD。 To enable DTD processing set the ProhibitDtd property on XmlReaderSettings to false and pass the settings into XmlReader.Create metho d.
要启用DTD处理,请将XmlReaderSettings上的ProhibitDtd属性设置为false,并将设置传递给XmlReader.Create方法。 at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseDoctypeDecl() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at writefile.Main() in C:\\Main\\C#June\\CH9\\CodeFile1.cs:line
在系统的System.Xml.XmlTextReaderImpl.ParseDocumentContent()的System.Xml.XmlTextRemplImpl.ParseDoctypeDecl()处的System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res,String arg)处的System.Xml.XmlTextReaderImpl.Throw(Exception e)处。在C:\\ Main \\ C#June \\ CH9 \\ CodeFile1.cs中的writefile.Main()处的Xml.XmlTextReaderImpl.Read()
I tried adding this before XmlReader.Create(fullpath) 我尝试在XmlReader.Create(fullpath)之前添加它
XmlReaderSettings settingsread = new XmlReaderSettings();
settingsread.ProhibitDtd = false;
I still get the same error, what's the real problem in this program? 我仍然得到同样的错误,这个程序中的真正问题是什么?
I believe you would need to change your reader create to reference the settings 我相信您需要更改您的读者创建以参考设置
XmlReader reader = XmlReader.Create(fullpath);
should become 应该成为
XmlReader reader = XmlReader.Create(fullpath, settingsread);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.