CDATA in XML & Read it using C#

CDATA is used for saving/storing unparced character data.Whatevrr data which we given in CDATA section which will eleminate by XML Parser. CDATA is usefull for storing the HTML,Javascript,XAML data. CDATA is used about text data that should not be parsed by the XML parser. Characters like "<" and "&" are illegal in XML elements."<" will generate an error because the parser interprets it as the start of a new element."&" will generate an error because the parser interprets it as the start of an character entity.
A CDATA section starts with "<![CDATA[" and ends with "]]>":

CDATA STORIng example.

  <EmailTemplate>
    <Template name="PaymentCreate">
      <![CDATA[<div style=' border:solid 1px #99FFFF' runat='server'><div style='background-color:#95aa77'>
    <p style='margin:0in;font-size:17.0pt;>
    <span style='font-weight:bold;font-family: Courier New CE'>Dear #UserName#</span></p><p style='margin:0in;font-family:Times New Roman;font-size:11.0pt'>#DateTime#</p>
    </div>
    <div style='border:solid 1px #c3c3c3;background-color:#CCFFFF'></br><p style='margin:0in;font-family:Times New Roman;font-size:14.0pt;color:#993300;'>
    <span style='font-weight:bold;font-family: Courier New CE'>Your Payment Voucher has been generated for Month of #Month# Year #Year#.</span></p>
    <p style='margin:0in;font-family:Times New Roman;font-size:14.0pt;color:#993300'></p>
    <p style='margin:0in;font-family:&quot;Times New Roman&quot;font-size:9.5pt;color:#2E75B5'>
    </br><span style='font-weight:bold;font-family: Courier New CE'>Amount<span style='mso-spacerun:yes'></span>:<span style='color:red;'> #Amount#</span></span></p>
    <p style='margin:0in;font-family:&quot;Times New Roman&quot;font-size:9.5pt;color:#2E75B5'>
    <span style='font-weight:bold;font-family: Courier New CE'>Last Date<span style='mso-spacerun:yes'></span>:<span style='color:red;'>#LastDate#</span>
    <span style='mso-spacerun:yes'></span></span></p><p style='margin:0in;font-family:&quot;Times New Roman&quot;font-size:9.5pt;color:#2E75B5'>
    <span style='font-weight:bold;font-family: Courier New CE'>Your E-Collections Account number : #AccountNumber#</span></p>
    <p style='margin:0in;font-family:Times New Roman;font-size:14.0pt;color:#993300'>&nbsp;</p></div>
    <div style='background-color:#95aa77'>
    <p style='margin:0in;font-family:Times New Roman;font-size:11.0pt;color:#375623'>
    <span style='font-weight: bold; '>The Owners’ Association</span></p>
    <p style='margin:0in;font-size:11.0pt'><span style='font-weight: bold; font-family: Times New Roman; color: #375623; '>Reg No</span>
    <span style='font-family: Century; color: #538135; '>:126/2013</span></p><p style='margin:0in;font-size:11.0pt'>
    <span style='font-family: Times New Roman; '>OMR Road </span><span style='font-family: Calibri; '> Kalavakkam</span>
    <span style='font-family: '>-</span><span style='font-family: Calibri; '>Tiruporur</span><span style='font-family:  '>-</span><span style='font-family: Calibri; '>Chennai</span><span style='font-family:  '>-</span><span style='font-family: Calibri; '>Tamil Nadu</span><span style='font-family: '>-</span><span style='font-family: Calibri; '>PIN: 603110 </span></p><p style='margin:0in;font-family:Times New Roman;font-size:11.0pt'><span style='font-weight: bold; color: #375623; '>Phone</span><span>: +91-0000000000 </span> </p><p style='margin:0in;font-family:Times New Roman;font-size:11.0pt'><span style='font-weight: bold; color: #375623; '>E-Mail</span><span style=''>: </span></p><p style='margin:0in;font-family:Times New Roman;font-size:11.0pt'><span style='font-weight: bold; color: #375623; '>WebSite</span><span style=''>:www.google.com</span></p></div></div>]]>
    </Template>
  </EmailTemplate>

We can read the CDATA using C#:

XElement XTemp = XElement.Load("EmailTemplate.xml");
                var queryCDATAXML = from element in XTemp.DescendantNodes()
                                    where element.NodeType == System.Xml.XmlNodeType.CDATA
                                    select element.Parent.Value.Trim();
                string BodyHtml = queryCDATAXML.ToList<string>()[0].ToString(); 

The above code will used for reading thae above CDATA. You can save the xml in your System and use the code.This will be usefull for storing templates in HTML,WPF & Silverlight .Also we can use this for storing functions like javascript....

Happy Doing.......

 

No comments:

Post a Comment