5

我正在做一个项目,在该项目中,我从 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();
        }
    }

对不起,大规模的方法,但在这种方法中,它会引发错误。任何帮助将不胜感激谢谢。

4

2 回答 2

9

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

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

于 2012-05-25T13:50:45.733 回答
1

你调用 startelement 81 次......猜猜看:你没有调用 endelement 81 次......

所以确保开始和结束正确匹配;)

于 2012-05-25T10:14:18.413 回答