在C#中编写XML-“处于Epilog状态的令牌StartElement将导致无效的XML文档"错误 [英] Writing XML in C# - 'Token StartElement in state Epilog would result in an invalid XML document' error

查看:105
本文介绍了在C#中编写XML-“处于Epilog状态的令牌StartElement将导致无效的XML文档"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写入XML文档,并从列表的ArrayList中获取数据

I'm trying to write to an XML document, taking data from an ArrayList of lists

        for (int i = 0; i < 15; i++)
        {
            string headname = (headers[0] as List<string>)[i];
                headname.Replace("&", "&amp;");
                headname.Replace("<", "&lt;");
                headname.Replace(">", "&gt;");
                headname.Replace("\"", "&quot;");
                headname.Replace("'", "&apos;");
            textWriter.WriteStartElement("MetadataName", "");
            textWriter.WriteString(headname);
            textWriter.WriteEndElement();

我遇到的问题是,它经过一次for循环后,在WriteStartElement的行上收到处于Epilog状态的Token StartElement会导致无效的XML文档"错误,并且我尝试查找如何解决它,还没有真正的突破.谢谢.

The problem I'm having is that after it goes through the for loop once, i get a 'Token StartElement in state Epilog would result in an invalid XML document' error on the line with WriteStartElement, and I've tried looking up how to fix it and have not really had any breakthroughs. Thanks.

已编辑以进行更新.

推荐答案

您的问题在这里:

textWriter.WriteStartElement("Metadata Name", "");

元素名称不能包含空格.试试这个:

An element name cannot contain spaces. Try this:

textWriter.WriteStartElement("MetadataName", "");

我假设您的 headers 数组中的数据对于XML而言格式正确(例如,所有& 都转义为& 等...).

I am assuming that the data in your headers array is well formed for XML (for example, all & are escaped to &amp; etc...).

另外,正如Mark在回答中指出的那样,您需要确保XML植根-即存在一个根元素,您可以在其中放置 MetadataName 元素.

Additionally, as Mark notes in his answer, you need to make sure the XML is rooted - that is, that there is a root element in which you put your MetadataName elements.

了解格式正确的XML 以及这意味着什么-在构建文档时,需要确保文档格式正确.

Read about well formed XML and what that means - you need to make sure your document is well formed, as you build it up.

这篇关于在C#中编写XML-“处于Epilog状态的令牌StartElement将导致无效的XML文档"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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