状态为 EndRootElement 的令牌 StartElement 将导致无效的 XML 文档

本文关键字:无效 XML 文档 StartElement EndRootElement 令牌 状态 | 更新日期: 2023-09-27 18:35:23

我正在做一个项目,当我从 c# 创建一个 Power Point 文档时。一切都很顺利,但由于某种原因,在我的一种方法上,我收到此错误:

"状态为 EndRootElement 的令牌 StartElement 将导致无效的 XML 文档。

这是我的方法:

 private static void slideLayout6XML()
    {
        FileStream slideLayout6Stream = new FileStream( slideLayout6, FileMode.Create, FileAccess.Write );
        XmlWriter slideLayout6Writer = null;
        try
        {
            XmlWriterSettings contentTypesSettings = new XmlWriterSettings();
            contentTypesSettings.Indent = true;
            slideLayout6Writer = XmlWriter.Create( slideLayout6Stream, contentTypesSettings );
            slideLayout6Writer.WriteStartDocument( true );
            slideLayout6Writer.WriteStartElement( "p", "sldLayout" );
            slideLayout6Writer.WriteAttributeString( "preserve", "1" );
            slideLayout6Writer.WriteAttributeString( "type", "titleOnly" );
            slideLayout6Writer.WriteStartElement( "p", "http://schemas.openxmlformats.org/drawingml/2006/main" );
            slideLayout6Writer.WriteStartElement( "r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships" );
            slideLayout6Writer.WriteStartElement( "a", "http://schemas.openxmlformats.org/presentationml/2006/main" );
            slideLayout6Writer.WriteStartElement( "p", "cSld" );
            slideLayout6Writer.WriteAttributeString( "name", "Title Only" );
            slideLayout6Writer.WriteStartElement( "p", "spTree" );
            slideLayout6Writer.WriteStartElement( "p", "nvGrpSpPr" );
            slideLayout6Writer.WriteStartElement( "p", "cNvpr" );
            slideLayout6Writer.WriteAttributeString( "name", "" );
            slideLayout6Writer.WriteAttributeString( "id", "1" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "cNvGrpSpPr" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "nvPr" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "grpSpPr" );
            slideLayout6Writer.WriteStartElement( "a", "xfrm" );
            slideLayout6Writer.WriteStartElement( "a", "off" );
            slideLayout6Writer.WriteAttributeString( "y", "0" );
            slideLayout6Writer.WriteAttributeString( "x", "0" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "ext" );
            slideLayout6Writer.WriteAttributeString( "y", "0" );
            slideLayout6Writer.WriteAttributeString( "x", "0" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "chOff" );
            slideLayout6Writer.WriteAttributeString( "y", "0" );
            slideLayout6Writer.WriteAttributeString( "x", "0" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "chExt" );
            slideLayout6Writer.WriteAttributeString( "y", "0" );
            slideLayout6Writer.WriteAttributeString( "x", "0" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "sp" );
            slideLayout6Writer.WriteStartElement( "p", "nvSpPr" );
            slideLayout6Writer.WriteStartElement( "p", "cNvPr" );
            slideLayout6Writer.WriteAttributeString( "name", "Title 1" );
            slideLayout6Writer.WriteAttributeString( "id", "2" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "cNvSpPr" );
            slideLayout6Writer.WriteStartElement( "a", "spLocks" );
            slideLayout6Writer.WriteAttributeString( "noGrp", "1" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "nvPr" );
            slideLayout6Writer.WriteStartElement( "p", "ph" );
            slideLayout6Writer.WriteAttributeString( "type", "title" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "txBody" );
            slideLayout6Writer.WriteStartElement( "a", "bodyPr" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "lstStyle" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "p" );
            slideLayout6Writer.WriteStartElement( "a", "r" );
            slideLayout6Writer.WriteStartElement( "a", "rPr" );
            slideLayout6Writer.WriteAttributeString( "lang", "en-US" );
            slideLayout6Writer.WriteAttributeString( "smtClean", "0" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "t" );
            slideLayout6Writer.WriteRaw( "Click to edit Master title style" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "endParaRPr" );
            slideLayout6Writer.WriteAttributeString( "lang", "en-US" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "sp" );
            slideLayout6Writer.WriteStartElement( "p", "nvSpPr" );
            slideLayout6Writer.WriteStartElement( "p", "cNvPr" );
            slideLayout6Writer.WriteAttributeString( "name", "Date Placeholder 2" );
            slideLayout6Writer.WriteAttributeString( "id", "3" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "cNvSpPr" );
            slideLayout6Writer.WriteStartElement( "a", "spLocks" );
            slideLayout6Writer.WriteAttributeString( "noGrp", "1" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "nvPr" );
            slideLayout6Writer.WriteStartElement( "p", "ph" );
            slideLayout6Writer.WriteAttributeString( "type", "dt" );
            slideLayout6Writer.WriteAttributeString( "idx", "10" );
            slideLayout6Writer.WriteAttributeString( "sz", "half" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "txBody" );
            slideLayout6Writer.WriteStartElement( "a", "bodyPr" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "lstStyle" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "p" );
            slideLayout6Writer.WriteStartElement( "a", "fld" );
            slideLayout6Writer.WriteAttributeString( "type", "datetime1" );
            slideLayout6Writer.WriteAttributeString( "id", "{32E0D616-E0F4-45FD-A59D-58900C81D413}" );
            slideLayout6Writer.WriteStartElement( "a", "rPr" );
            slideLayout6Writer.WriteAttributeString( "lang", "en-US" );
            slideLayout6Writer.WriteAttributeString( "smtClean", "0" );
            slideLayout6Writer.WriteEndElement();
            
            slideLayout6Writer.WriteStartElement( "a", "t" );
            slideLayout6Writer.WriteRaw( "5/17/2012" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "endParaRpR" );
            slideLayout6Writer.WriteAttributeString( "lang", "en-US" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "sp" );
            slideLayout6Writer.WriteStartElement( "p", "nvSpPr" );
            slideLayout6Writer.WriteStartElement( "p", "cNvPr" );
            slideLayout6Writer.WriteAttributeString( "name", "Footer Placeholder 3" );
            slideLayout6Writer.WriteAttributeString( "id", "4" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "cNvSpPr" );
            slideLayout6Writer.WriteStartElement( "a", "spLocks" );
            slideLayout6Writer.WriteAttributeString( "noGrp", "1" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "nvPr" );
            slideLayout6Writer.WriteStartElement( "p", "ph" );
            slideLayout6Writer.WriteAttributeString( "type", "ftr" );
            slideLayout6Writer.WriteAttributeString( "idx", "11" );
            slideLayout6Writer.WriteAttributeString( "sz", "quarter" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "txBody" );
            slideLayout6Writer.WriteStartElement( "a", "bodyPr" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "lstStyle" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "p" );
            slideLayout6Writer.WriteStartElement( "a", "r" );
            slideLayout6Writer.WriteStartElement( "a", "rPr" );
            slideLayout6Writer.WriteAttributeString( "lang", "en-US" );
            slideLayout6Writer.WriteAttributeString( "smtClean", "0" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "t" );
            slideLayout6Writer.WriteRaw( "Crowded Igloo Business System" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "endParaRPr" );
            slideLayout6Writer.WriteAttributeString( "lang", "en-US" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "sp" );
            slideLayout6Writer.WriteStartElement( "p", "nvSpPr" );
            slideLayout6Writer.WriteStartElement( "p", "cNvPr" );
            slideLayout6Writer.WriteAttributeString( "name", "Slide Number Placeholder 4" );
            slideLayout6Writer.WriteAttributeString( "id", "5" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "cNvSpPr" );
            slideLayout6Writer.WriteStartElement( "a", "spLocks" );
            slideLayout6Writer.WriteAttributeString( "noGrp", "1" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "nvPr" );
            slideLayout6Writer.WriteStartElement( "p", "ph" );
            slideLayout6Writer.WriteAttributeString( "type", "sldNum" );
            slideLayout6Writer.WriteAttributeString( "idx", "12" );
            slideLayout6Writer.WriteAttributeString( "sz", "quarter" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "txBody" );
            slideLayout6Writer.WriteStartElement( "a", "bodyPr" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "lstStyle" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "p" );
            slideLayout6Writer.WriteStartElement( "a", "fld" );
            slideLayout6Writer.WriteAttributeString( "type", "slidenum" );
            slideLayout6Writer.WriteAttributeString( "id", "{8B131516-2DB0-4AAA-89BA-2724DE07B6A2}" );
            
            slideLayout6Writer.WriteStartElement( "a", "rPr" );
            slideLayout6Writer.WriteAttributeString( "lang", "en-US" );
            slideLayout6Writer.WriteAttributeString( "smtClean", "0" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "t" );
            slideLayout6Writer.WriteRaw( "<#>" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "a", "endParaRPr" );
            slideLayout6Writer.WriteAttributeString( "lang", "en-US" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "extLst" );
            slideLayout6Writer.WriteStartElement( "p", "ext" );
            slideLayout6Writer.WriteAttributeString( "uri", "{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}" );
            slideLayout6Writer.WriteStartElement( "p14", "creationId", "http://schemas.microsoft.com/office/powerpoint/2010/main" );
            slideLayout6Writer.WriteAttributeString( "val", "3805825225" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteStartElement( "p", "clrMapOvr" );
            slideLayout6Writer.WriteStartElement( "a", "masterClrMapping" );
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            slideLayout6Writer.WriteEndElement();
            
            slideLayout6Writer.WriteEndDocument();
        }
        finally
        {
            if (slideLayout6Writer != null)
                slideLayout6Writer.Flush();
            slideLayout6Writer.Close();
        }
    }

很抱歉使用大量方法,但在此方法中它会抛出错误。

状态为 EndRootElement 的令牌 StartElement 将导致无效的 XML 文档

我的猜测是你已经关闭了最外层的元素,所以不允许你打开一个新元素,因为XML只允许最外层的一个元素。

这种接口对于编写XML非常方便,我自己也使用它,但是如果您犯了任何嵌套错误,则调试起来可能会很麻烦。众所周知,我对接口使用自己的包装器,其中对 endElement 的调用必须指定匹配的标签名称;然后,包装器可以检查调用的正确嵌套,这有助于识别错误的位置。

你调用 startelement 81 次......你猜怎么着:你没有调用 endelement 81 次......

因此,请确保开始和结束正确匹配;)