在状态End Document中获取错误Token StartDocument会导致XML文档无效

时间:2015-06-22 07:46:23

标签: c# .net xml xml-parsing xmlwriter

我正在努力创造这个:

<?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>

我的代码如下:

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) ;             
        }

当我尝试运行IIS的代码时,我收到一条错误消息:

 <?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

我仔细检查了所有内容是否正确打开和关闭。

2 个答案:

答案 0 :(得分:1)

问题是您需要在循环外移动文档和根元素的创建:

            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();

否则,如果您有多个符合条件activity.Info1.ToLower().Contains(FileName.ToLower())的文件,那么您将尝试创建一个包含多个root elements的XML文档,这是XML标准所不允许的。

答案 1 :(得分:0)

XML区分大小写。尝试下面的更正

<?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>
​