我用 编写了一个 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新手,所以请耐心等待。