k

k

  • NA
  • 25
  • 0

Problem with XmlTextWriter

May 23 2009 6:34 PM
I had the code to write data into xml file. But I got 01 error when running. But I don't know why. Please show me the reason.

            /////////////////////////WRITE TO XML FILE/////////////////////////////
            XmlTextWriter myXmlTextWriter = new XmlTextWriter("manchester_united.xml", null);
            myXmlTextWriter.Formatting = Formatting.Indented;                       
            myXmlTextWriter.WriteStartDocument(false);
            myXmlTextWriter.WriteDocType("bookstore", null, "books.dtd", null);
            myXmlTextWriter.WriteComment("my data");
            myXmlTextWriter.WriteStartElement("Data");                          
            string getString = "";
            int n;
            flag = 0;
            for (int i = 0; i < countLink; i++)       
            {
                for (n = 0; n < url.Length; n++)
                {
                    if (link[i]._originalLink == url[n].link)
                    {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 1)
                {               
                 myXmlTextWriter.WriteStartElement("WebPage", null); //When i ==0, there was no error, but when i ==1, I got error in this line (*)
                        myXmlTextWriter.WriteAttributeString("Index", i.ToString());
                        myXmlTextWriter.WriteAttributeString("RootSet", "True");
                            myXmlTextWriter.WriteStartElement("Link");
                                myXmlTextWriter.WriteAttributeString("Title", "");
                                myXmlTextWriter.WriteString(url[n].link);
                            myXmlTextWriter.WriteEndElement();
                                for (int j = 0; j < link[i]._countEmbeddedLink; j++)       
                                    for (int m = 0; m < url.Length; m++)
                                    {
                                        if (url[m].link == link[i]._embeddedLink[j])
                                            getString = getString + m.ToString() + " "; 
                                    }                
                            myXmlTextWriter.WriteElementString("List", getString);                           
                    myXmlTextWriter.WriteEndElement();                                       
                }
            }
            myXmlTextWriter.WriteEndElement();
            myXmlTextWriter.Flush();
            myXmlTextWriter.Close();

(*) the error is:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
Additional information: Token StartElement in state Epilog would result in an invalid XML document.


Thanks all in advance.