TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

What Is CDATA In XML ? (What Is Character Data/ Unparsed Data in XML?)

CDATA is used to store text data in XML file so that it should not be parsed by any XML parser.DATA are used to escape blocks of text containing characters which would otherwise be treated as markup.

As explained in my previous article Parsed Data(PDATA) , all XML parsers normally parse the data inside the xml elements tag  also.

There won´t be  any issue if all the data elements are having normal characters and parser will give correct result.But there are some special escape characters(&,`,”,<,> ) in XML which when parsed will give some unexpected results and hence normal practice is to convert these escape characters to corresponding XML characer and store in XML . Here comes the use of CDATA.

CDATA is used to store text data in XML file so that it should not be parsed by any XML parser. Means, CDATA are used to escape blocks of text containing characters which would otherwise be treated as markup.

For example , <Tag&> when stored normal way  in XML file as shown below

</Tag> <Tag&> </Tag> 

will give unexpected results during parsing since that also will be considered as markup. But the above parse issue can be rectified by storing with CDATA as below.

</Tag>

<![CDATA[

<Tag&>

]]>

</Tag>

Here <Tag&> will not be parsed.It will be considered as pure character data.So whenever you want treat a section of the XML element value as pure character data and want to skip that during parsing go for CDATA. Nested CDATA sections are not allowed.

Some examples below.

1)

<?xmlversion="1.0"encoding="utf-8" ?>

<TagName>

  <![CDATA[

    <Fragment>

        <Value> ><&&&><´<” <Value>

    </Fragment>

  ]]>

</TagName>

In the above example entire <Fragment> section will not be parsed.

2) An entire code block can be written as CDATA section.

<?xmlversion="1.0"encoding="utf-8" ?>

<CodeBlock>

  <![CDATA[

internal static List<string> GetXmlElementValues(string xmlFilePath,string xPath)

        {      

            List<string> valueList = new List<string>();

            //XPathDocument document = new XPathDocument(@"E:\Demos\XpathNavigatorSample\XpathNavigatorSample\Book.xml");

            XPathDocument document = new XPathDocument(xmlFilePath);

            XPathNavigator navigator = document.CreateNavigator();

            //XPathNodeIterator nodes = navigator.Select("MessageResource/Message/MessageBlock/MessageText");

            XPathNodeIterator nodes = navigator.Select(xPath);

            foreach (XPathNavigator item in nodes)

            {

                valueList.Add(item.InnerXml);             

            }

            return valueList;

        }

]]>

</CodeBlock>

Following posts might also be imteresting to you..

Read CDATA Values From XML 

How To Store Javascript In XML File

 

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading