保存xml文件和ConformanceLevel [英] saving xml file and ConformanceLevel

查看:94
本文介绍了保存xml文件和ConformanceLevel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将对象列表保存到xml文件中的方法

I have method which should save list of objects into xml file

 private void DumpToXMLFile(List<Url> urls, string fileName)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            settings.NewLineOnAttributes = true;
            settings.ConformanceLevel = ConformanceLevel.Auto;

            using (XmlWriter writer = XmlWriter.Create(fileName, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Countries");
                foreach (var url in urls)
                {
                    writer.WriteStartElement("Country");
                        writer.WriteElementString("Name", url.Name);                        
                        writer.WriteElementString("Url", url.Uri);
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                writer.WriteEndElement();                
            }
        }

我正在获得这个期望:

类型为'System.InvalidOperationException'的未处理异常发生在...

An unhandled exception of type 'System.InvalidOperationException' occurred in ...

其他信息:状态为EndRootElement的令牌EndElement将导致无效的XML文档.确保ConformanceLevel设置设置为ConformanceLevel.Fragment或ConformanceLevel.Auto如果要编写XML片段.

Additional information: Token EndElement in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.

尝试使用ConformanceLevel.Fragment,但比起我要保存XML文件时应该使用ConformanceLevel.Auto的例外.

Tried with ConformanceLevel.Fragment but than I'm getting exception that I should use ConformanceLevel.Auto if I want to save xml file.

推荐答案

错误消息是:

处于EndRootElement状态的令牌EndElement将导致无效的XML文档.

Token EndElement in state EndRootElement would result in an invalid XML document.

换句话说,您要在没有其他要关闭的内容(您已经到达文档根目录)的情况下尝试编写end元素.

In other words, you are trying to write an end element when there is nothing else to close (you have already arrived at the document root).

因此,看看关闭任何元素的位置:

Hence, look at where you close any elements:

writer.WriteEndElement();
writer.WriteEndElement();

您将在其中关闭两个元素,但仅打开一个一个元素(< Countries> ):

You are Closing two elements there, but you open only one element (<Countries>):

writer.WriteStartDocument();
writer.WriteStartElement("Countries");

WriteStartDocument 不会启动Xml元素,它只是写文档Xml声明(例如,<?xml version ="1.0" encoding ="UTF-8"?> ).

WriteStartDocument does not start an Xml element, it just writes the document Xml declaration (e.g. <?xml version="1.0" encoding="UTF-8"?>).

删除第二个 writer.WriteEndElement(); ,您应该没事.

Remove the second writer.WriteEndElement(); and you should be fine.

这篇关于保存xml文件和ConformanceLevel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆