xml 数据总是被创建为 CDATA 而不是 PCDATA

xml data always being created as CDATA and not PCDATA

我用 C# 写了一个网络服务,我想要他的方法之一 return 和 XML。 我设法做到了,但所有数据都被标记为 CDATA 而未被解析。这不是我要找的。

这是我的代码:

 [WebMethod(EnableSession = true, Description = "Returns the safe activities for the required days period in XML")]
    public string GetSafeActivitiesXML(string safename, int days, string FileName)
    {
        string returnErrorCode = "001";
        try
        {
            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent = true
                //IndentChars = "  ",
                //NewLineChars = "\n",
                //NewLineHandling = NewLineHandling.None,
                //Encoding = System.Text.Encoding.UTF8
            };

            StringWriter sb = new StringWriter();
            XmlWriter writer = XmlWriter.Create(sb,settings);

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

            int lineCouner = 0;

            if (safeActivities.Count > 0)
            {
                writer.WriteStartElement("ListOfStrings", "");
                foreach (ActivityLogRecord activity in safeActivities)
                {
                        writer.WriteStartElement("string");
                        writer.WriteElementString("outFileName", (activity.Info1.Substring(activity.Info1.LastIndexOf("\")+1)));
                        writer.WriteElementString("activityTmStamp", activity.Time.ToString());
                        writer.WriteElementString("userName", activity.UserName);
                        writer.WriteElementString("ActionID", activityCode);
                        writer.WriteElementString("direction", direction);
                        writer.WriteElementString("path", activity.Info1);
                        writer.WriteEndElement();
                        lineCouner++;
                    }
                 }
                writer.WriteEndElement();
            }

            writer.WriteStartElement("retunCode");
            writer.WriteString((lineCouner > 0) ? "0" : "2");
            writer.WriteEndElement();
            writer.WriteStartElement("retunMessage");
            writer.WriteString((lineCouner > 0) ? "תקין" : "אין נתונים");
            writer.WriteEndElement();

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

            XmlDocument xmlOut = new XmlDocument();

            xmlOut.LoadXml(sb.ToString());
            writer.Close();
            //xmlOut.Save(xxx);
            string finalOutput = sb.ToString();
            finalOutput.Replace("![CDATA[", "").Replace("]]", "");
            return sb.ToString();

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

    }

这是当前输出的示例:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
  <GetSafeActivitiesXMLResponse xmlns="http://www.securenet.co.il">
     <GetSafeActivitiesXMLResult><![CDATA[<?xml version="1.0" encoding="utf-16"?>
  <GetSafeActivitiesResult>
   <ListOfStrings>
<string>
  <outFileName>code-xmp-tmp.txt</outFileName>
  <activityTmStamp>21/06/2015 10:58:38</activityTmStamp>
  <userName>naaman</userName>
  <ActionID>קובץ אוחסן בכספת</ActionID>
  <direction>Unknown</direction>
  <path>Root\fgdf\code-xmp-tmp.txt</path>
</string>
</ListOfStrings>
<retunCode>0</retunCode>
<retunMessage>תקין</retunMessage>
 </GetSafeActivitiesResult>]]></GetSafeActivitiesXMLResult>
   </GetSafeActivitiesXMLResponse>
</soap:Body>

这就是我想要达到的效果:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
  <GetSafeActivitiesXMLResponse xmlns="http://www.securenet.co.il">
     <GetSafeActivitiesXMLResult><?xml version="1.0" encoding="utf-16"?>
  <GetSafeActivitiesResult>
   <ListOfStrings>
<string>
  <outFileName>code-xmp-tmp.txt</outFileName>
  <activityTmStamp>21/06/2015 10:58:38</activityTmStamp>
  <userName>naaman</userName>
  <ActionID>קובץ אוחסן בכספת</ActionID>
  <direction>Unknown</direction>
  <path>Root\fgdf\code-xmp-tmp.txt</path>
</string>
</ListOfStrings>
<retunCode>0</retunCode>
<retunMessage>תקין</retunMessage>
 </GetSafeActivitiesResult></GetSafeActivitiesXMLResult>
   </GetSafeActivitiesXMLResponse>
</soap:Body>

所以我的问题真的是,如何去掉 CDATA 标签,为什么它会出现在第一位。

我是xml的新手,所以请耐心等待。

您想要实现的输出是 XML that is not well-formed: you're essentially trying to nest an XML document, complete with an XML declaration(即 <?xml version="1.0" encoding="utf-16"?>),作为 XML 文档(或片段)中的文字字符数据——这不是合法的构造.

包含 XML 文档或任何将被识别为 markup, within another XML document (element) is to basically escape it by using a CDATA section 的文本的正确方法,以便 解析为标记。而这正是网络 service/SOAP 基础设施为您所做的。

如果它没有这样做,并且您的 XML 文本变成了您想要的已解析数据 (PCDATA),则消费解析器将抛出异常或 return 错误,因为您的 Web 服务响应 XML 格式不正确。

方法 return 是一个 String 类型,这就是问题所在。 我将 return 类型更改为 XmlDocument,现在它是所有蜂蜜和坚果。