1

我用 编写了一个 Web 服务C#,并且我想要他的一种方法返回一个 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新手,所以请耐心等待。

4

2 回答 2

0

您想要实现的输出是格式不正确XML:您实际上是在尝试嵌套一个 XML 文档,其中包含一个XML 声明(即),作为 XML 文档(或片段)中的文字字符数据——这不是一个合法的结构。<?xml version="1.0" encoding="utf-16"?>

在另一个 XML 文档(元素)中包含 XML 文档或任何可以被识别为标记的文本的正确方法是通过使用一个CDATA部分来基本上对其进行转义,这样它就不会被解析为标记。这正是 Web 服务/SOAP 基础架构正在为您做的事情。

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

于 2015-06-30T08:17:22.400 回答
0

该方法返回一个字符串类型,这就是问题所在。我将返回类型更改为 XmlDocument,现在它全是蜂蜜和坚果。

于 2015-06-30T10:43:32.663 回答