读取和写入XML - DTD错误

时间:2010-06-28 16:53:32

标签: c# xml dtd xmlreader

我有一个程序,它使用XMLReader和XMLWriter

读写XML
 XmlWriter writer =
 XmlWriter.Create(fullpath, settings);

 //content...

 writer.Flush();

 writer.Close();

和我的读者代码

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

编写器工作正常,但是当它到达XmlReader reader = XmlReader.Create(fullpath);

它会打印两次未知类型的错误消息,然后崩溃并显示错误

  

未处理的例外情况:   System.Xml.XmlException:为了安全起见   原因DTD是禁止的       在这个XML文档中。要启用DTD处理设置   ProhibitDtd财产        在XmlReaderSettings上为false并将设置传递给   XmlReader.Create方法       d。          在System.Xml.XmlTextReaderImpl.Throw(例外   E)          在System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String   res,String arg)          在System.Xml.XmlTextReaderImpl.ParseDoctypeDecl()          在System.Xml.XmlTextReaderImpl.ParseDocumentContent()          在System.Xml.XmlTextReaderImpl.Read()          at writefile.Main()在C:\ Main \ C#June \ CH9 \ CodeFile1.cs:line

我尝试在XmlReader.Create(fullpath)

之前添加它
XmlReaderSettings settingsread = new XmlReaderSettings();
settingsread.ProhibitDtd = false;

我仍然得到同样的错误,这个程序中的真正问题是什么?

1 个答案:

答案 0 :(得分:4)

我相信您需要更改您的阅读器创建以引用设置

XmlReader reader = XmlReader.Create(fullpath);

应该成为

XmlReader reader = XmlReader.Create(fullpath, settingsread);