getting error Token StartDocument in state End Document would result in an invalid XML document

5.5k views Asked by At

i'm trying to create this:

<?xml version="1.0" encoding="utf-8" ?>
<GetsafeActivitiesResponse xmlns="http://ggg.co.il">
<GetSafeActivitiesResult>
<ListofStrings>
  <string>
    <timestamp>2015 03:44:11</timestamp>
    <user>bbbbb</user>
    <code>ccdcdcd</code>
  </string>
</ListofStrings>
<returncode>55555</returncode>
<returnMSG>sssss</returnMSG>
</GetSafeActivitiesResult>
</GetsafeActivitiesResponse>

my code looks like this:

XmlWriterSettings settings = new XmlWriterSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            settings.Indent = true;
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb, settings);

            foreach (ActivityLogRecord activity in safeActivities)
                {
                    if (activity.Info1.ToLower().Contains(FileName.ToLower()))
                    {
                        string activityCode = "";
                        string direction = "Unknown";
                        string direction = "ddd";

                        writer.WriteStartDocument();
                        writer.WriteStartElement("GetSafeActivitiesResult");
                        writer.WriteStartElement("ListOfStrings","");

                        int i = 0;
                        while (results.Length > i)
                        {
                            writer.WriteStartElement("string");
                            writer.WriteStartElement("outFileName");
                            writer.WriteString(FileName);
                            writer.WriteEndElement();
                            writer.WriteStartElement("activityTmStamp");
                            writer.WriteString(activity.Time.ToString());
                            writer.WriteEndElement();
                            writer.WriteStartElement("userName");
                            writer.WriteString(activity.UserName);
                            writer.WriteEndElement();
                            writer.WriteStartElement("ActionID");
                            writer.WriteString(activityCode);
                            writer.WriteEndElement();
                            writer.WriteStartElement("direction");
                            writer.WriteString(direction);
                            writer.WriteEndElement();
                            writer.WriteStartElement("path");
                            writer.WriteString(activity.Info1);
                            writer.WriteEndElement();
                            writer.WriteEndElement();

                            //results[i] = string.Format("<acivityTmStamp>{0}</acivityTmStamp><username>{1}</username><ActionID>{2}</ActionID><direction>{3}</direction><path>{4}</path>", activity.Time, activity.UserName, activityCode, direction, activity.Info1);

                            i++;
                        }

                        writer.WriteEndElement();

                        writer.WriteStartElement("retunCode");
                        writer.WriteString("000");
                        writer.WriteEndElement();
                        writer.WriteStartElement("retunMessage");
                        writer.WriteString("000");

                        writer.WriteEndElement();


                        writer.WriteEndElement();
                        writer.WriteEndDocument();
                        writer.Flush();

                    }
                }

            returnErrorCode = "005";
            if (safe != null)
                safe.Dispose();

            if (Vault.Current.IsLoggedOn()) 
                UserSession.Logoff();

            returnErrorCode = "006";
            XmlDocument xmlOut = new XmlDocument();
            xmlOut.LoadXml(sb.ToString());
            writer.Close();
            return xmlOut.InnerXml.ToString(); 

        }
        catch (Exception ex)
        {
            this.LogWrite("GetSafeActivities", string.Format("Operation has failed: {0}", ex.Message), Session.SessionID, true);
            return string.Format("<ReturnCode>{0}</ReturnCode><ReturnMSG>{1}</ReturnMSG>", returnErrorCode, ex.Message) ;             
        }

when i try and run the code vis IIS i get an error message:

 <?xml version="1.0" encoding="UTF-8"?>
 <string    xmlns="http://www.securenet.co.il"><ReturnCode>004</ReturnCode><ReturnMSG>Token StartDocument in state End Document would result in an invalid XML document.</ReturnMSG></string

i double checked that everything opens and closes correctly.

2

There are 2 answers

0
dbc On BEST ANSWER

The problem is that you need to move creation of the document and root element outside the loop:

            writer.WriteStartDocument();
            writer.WriteStartElement("GetSafeActivitiesResult");

            foreach (ActivityLogRecord activity in safeActivities)
            {
                if (activity.Info1.ToLower().Contains(FileName.ToLower()))
                {
                    // Write out the activity information
                }
            }

            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();

Otherwise, if you have multiple files that match the criterion activity.Info1.ToLower().Contains(FileName.ToLower()), then you will be trying to create an XML document with multiple root elements, which is not allowed by the XML standard.

2
jdweng On

XML is case sensitive. Try corrections below

<?xml version="1.0" encoding="utf-8" ?>
<GetsafeActivitiesResponse xmlns="http://ggg.co.il">
  <GetSafeActivitiesResult>
    <ListofStrings>
      <string>
        <timestamp>2015 03:44:11</timestamp>
        <user>bbbbb</user>
        <code>ccdcdcd</code>
      </string>
    </ListofStrings>
    <returncode>55555</returncode>
    <returnMSG>sssss</returnMSG>
  </GetSafeActivitiesResult>
</GetsafeActivitiesResponse>
​