Click here to Skip to main content
15,304,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,
Requirement is to add CData section to existing XML string of below format
XML
<ContactSet>
<Contacts>
	<Contact>
		<ContactID>33493</ContactID>
		<CustomerID>6663</CustomerID><Fax />
		<EmailID>xyz@gmail.com</EmailID>
		<DateNoMailRequested>1970-01-01T05:30:00</DateNoMailRequested>
		<Password>nJzVu@{YxE3</Password>
		<Phone>918043510100</Phone>
		<Summary>Hi!, Awaiting for your reply. Thanks & Regards</Summary>
	</Contact>
</Contacts>
</ContactSet>

Required output is to be like this
XML
<ContactSet>
<Contacts>
	<Contact>
		<ContactID><![CDATA[33493]]></ContactID>
		<CustomerID><![CDATA[6663]]></CustomerID><Fax />
		<EmailID><![CDATA[xyz@gmail.com]]></EmailID>
		<DateNoMailRequested><![CDATA[1970-01-01T05:30:00]]></DateNoMailRequested>
		<Password><![CDATA[nJzVu@{YxE3]]></Password>
		<Phone><![CDATA[918043510100]]></Phone>
		<Summary><![CDATA[Hi!, Awaiting for your reply. Thanks & Regards]]></Summary>
	</Contact>
</Contacts>
</ContactSet>


Basically without CData section when i try to load xml string into XDocument, .net is giving hex decimal parsing error.
Hope that adding CData section will solve the problem.

Awaiting for reply

Thanks in advance
Posted
Updated 7-Jun-16 6:35am
v3
Comments
JF2015 19-Jan-11 2:26am
   
Edited to add code formatting.

Hi,

use the WriteCData method of the XMLWriter class.


C#
using (XmlWriter writer = XmlWriter.Create("employees.xml"))
{
    writer.WriteStartElement("ContactID");
    writer.WriteCData(data);
    writer.WriteEndElement();
}


hope it helps.

Valery.
   
Comments
Madhu J N 19-Jan-11 3:35am
   
Hey thanks for your reply,
Above logic will helps while creating XML, in my case am not creating XML. Basically i'll get xml from outer system, to this xml string i need to add CData section before processing in my application.
C#
public string InsertCDATASections(DataSet ds, string[] cdataSections)
   {
       //Convert to XML with expanded general entities and CDATA sections
       //as appropriate
       XmlValidatingReader reader = null;
       XmlTextWriter writer = null;
       StringWriter sw = null;
       Array.Sort(cdataSections);
       try
       {
           reader = new XmlValidatingReader(ds.GetXml(), XmlNodeType.Document, null);
           sw = new StringWriter();
           writer = new XmlTextWriter(sw);
           writer.Formatting = Formatting.Indented;
           reader.ValidationType = ValidationType.None;
           reader.EntityHandling = EntityHandling.ExpandCharEntities;
           string currentElement = String.Empty;
           while (reader.Read())
           {
               switch (reader.NodeType)
               {
                   case XmlNodeType.Element:
                       currentElement = reader.Name;
                       writer.WriteStartElement(currentElement);
                       while (reader.MoveToNextAttribute())
                       {
                           writer.WriteAttributeString(reader.Name, reader.Value);
                       }
                       break;
                   case XmlNodeType.Text:
                       if (Array.BinarySearch(cdataSections, currentElement) < 0 && cdataSections.Length > 0 )
                       {
                           writer.WriteString(reader.Value);
                       }
                       else
                       {
                           writer.WriteCData(reader.Value);
                       }
                       break;
                   case XmlNodeType.EndElement:
                       writer.WriteEndElement();
                       break;
                   default:
                       break;
               }
           }
       }
       catch (Exception exp)
       {
           return exp.Message;
       }
       finally
       {
           reader.Close();
           writer.Close();
       }
       return sw.ToString();
   }


Example:
DataSet ds = new DataSet();
string[] sections = new string[]{};

string xml = InsertCDataSections(ds, sections);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900