Writing XML in C# - 'Token StartElement in state Epilog would result in an invalid XML document' error

后端 未结 2 546
Happy的楠姐
Happy的楠姐 2021-01-12 06:32

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         


        
相关标签:
2条回答
  • 2021-01-12 07:00

    Your problem is here:

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

    An element name cannot contain spaces. Try this:

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

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

    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.

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

    0 讨论(0)
  • 2021-01-12 07:14

    An XML document will have a root element ie one that includes all other elements. You don't have one

    Your doc is like

    <MetadataName><header0/><header1/></MetadataName>
    <MetadataName><header0/><header1/></MetadataName>
    

    An XML doc is like

    <root>
        <MetadataName><header0/><header1/></MetadataName>
        <MetadataName><header0/><header1/></MetadataName>
    </root>
    

    You need to WriteStartElement and WriteEndElement around your loop

    0 讨论(0)
提交回复
热议问题