Hi all

i can navigate through my xml document no problem and can edit some nodes by using the setValue method.

here is part of my xml output

< xml version="1.0" encoding="UTF-8" >
< xml-stylesheet type='text/xsl' href='search/dsp_case.xsl' >

the following code navigates to to enc node and sets the value to UTF-8

xmlCaseIdTemplate.PreserveWhitespace = false;

XPathNavigator myXPathNavigator = xmlCaseIdTemplate.CreateNavigator();



//navagate to qvc element and insert the "utf-8" encoded data

myXPathNavigator.MoveToChild("qvc", "");



that works fine but my problem is i am unable to set the nodes with charactor data.

I tried this


XmlCDataSection CData = xmlCaseIdTemplate.CreateCDataSection("28");


but the output i get is


but i really want something like


Is this possable or is there a work around

please help me as ASAP .

Re: XML and the .NET Framework need help quick with myXPathNavigator

Sergey Dubinets - MSFT

CData.Value has type string and


is absolutely equivalent to


So why did you expect it to work

You can't set/get CDATA through XPathNavigator. XPath Data Model don't have CDATA sections. It is considered serialization hit and is not available through XPath primitives.

You have three ways to solve the problem.

1. (The best) Don't mess with CDATA :-)

2. (XmlNode) XPathNavigator.UnderlyingObject gives you back DOM node and you can set/get CDATA using DOM interface.

3. When you writing XML with XmlWriterSettings you can specify witch elements should be writting using CDATA. Not quite intuiteve but works:

XslCompiledTransform t = new XslCompiledTransform();

t.Load(new StringReader(@"

<xsl:stylesheet version='1.0' xmlns:xsl=''>

<xsl:output indent='yes' method='xml' cdata-section-elements='caseid' />



XmlWriter.Create("myFile.xml", t.OutputSettings);

4. Most value of XPathNavigator in the XPath language. MyXPathNavigator. SelectSingleNode(wfmcas/qvc/enc) XmlDocument also capable of executing XPath expresstions.

Re: XML and the .NET Framework need help quick with myXPathNavigator


ok i went for option 2.


thanks a million for your help. I changed my code to this.




XmlCDataSection CData = xmlCaseIdTemplate.CreateCDataSection(caseid);

XmlNode node = (XmlNode)myXPathNavigator.UnderlyingObject;

node.ReplaceChild(CData, node.FirstChild);