开发者

Reading and writing to an XML - DTD error

开发者 https://www.devze.com 2023-01-04 20:05 出处:网络
I have a program where it reads and writes XML using XMLReader and XMLWriter XmlWriter writer = XmlWriter.Create(fullpath, settings);

I have a program where it reads and writes XML using XMLReader and XMLWriter

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

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. To enable DTD processing set the ProhibitDtd property on XmlReaderSettings to false and pass the settings into XmlReader.Create metho d. 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

I tried adding this before XmlReader.Create(fullpath)

XmlReaderSettings settingsread = new XmlReaderSettings();
settingsread.ProhibitDtd =开发者_StackOverflow社区 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);
0

精彩评论

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