状态EndRootElement中的Token StartElement将导致无效的XML文档

时间:2012-05-25 10:01:26

标签: c# xml powerpoint

我正在开发一个项目,当我从c#创建一个power point文档时。 由于某些原因我的一个方法得到了这个错误,所以ging很好:

“状态EndRootElement中的Token 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();
        }
    }

很抱歉这个庞大的方法,但在这种方法中它会抛出错误。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:6)

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

这种界面对于编写XML非常方便,而且我自己也使用它,但是如果你犯了任何嵌套错误,它可能是一个调试的猪。我已经知道使用我自己的包装器来接口,其中对endElement的调用必须指定匹配的标签名称;然后,包装器可以检查调用的正确嵌套,这有助于识别错误的位置。

答案 1 :(得分:1)

你打电话给startelement 81次..猜猜:你没有打电话给81次......

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