【问题标题】:xml data always being created as CDATA and not PCDATAxml 数据总是被创建为 CDATA 而不是 PCDATA
【发布时间】:2015-09-16 20:48:01
【问题描述】:

我用C# 编写了一个Web 服务,我希望他的一个方法返回一个XML。 我已经设法做到了,但所有数据都被标记为CDATA 并且没有被解析。这不是我要找的。​​p>

这是我的代码:

 [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新手,请耐心等待。

【问题讨论】:

    标签: c# xml soap xmlwriter pcdata


    【解决方案1】:

    您想要实现的输出是XML,而不是well-formed:您实际上是在尝试嵌套一个XML 文档,其中包含一个XML declaration(即&lt;?xml version="1.0" encoding="utf-16"?&gt;),作为文本字符数据在一个XML 文档(或片段)——这不是合法的构造。

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

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

    【讨论】:

    • 嗨,我真的不需要这个 "" 我只需要结构可以解析 AKA PCDATA,我能做什么达到那个目的?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 2018-03-22
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      相关资源
      最近更新 更多