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.
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.
<Value> ><&&&><´<” <Value>
In the above example entire <Fragment> section will not be parsed.
2) An entire code block can be written as CDATA section.
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)
Following posts might also be imteresting to you..