I wrote a web service with C#
, and i want one of his methods to return an XML.
i've managed to do so , but all the data is being tagged as CDATA
and not being parsed. its not what i am looking for.
this is my code :
[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) ;
}
}
This is an example for the current output:
<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>
This is what i want to achieve:
<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>
so my question really is , how to get rid of the CDATA tag, and why is it there on the first place.
i'm new to xml, so please be patient.
the method returned a String type, and that was the problem. i changed the return type to XmlDocument, and now its all honey and nuts.