redchord2.0

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' >
<wfmcase>
<qvc>
<enc>UTF-8</enc>
</qvc>
<case_details>
<caseid>28</caseid>

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

xmlCaseIdTemplate.PreserveWhitespace = false;

XPathNavigator myXPathNavigator = xmlCaseIdTemplate.CreateNavigator();

myXPathNavigator.MoveToRoot();

myXPathNavigator.MoveToFirstChild();

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

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

myXPathNavigator.MoveToFirstChild();

myXPathNavigator.SetTypedValue("UTF-8");

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

I tried this

myXPathNavigator.MoveToFirstChild();

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

myXPathNavigator.SetTypedValue(CData.Value);

but the output i get is

<caseid>28</caseid>

but i really want something like

<caseid><![CDATA[28]]></caseid>

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

myXPathNavigator.SetTypedValue(CData.Value);

is absolutely equivalent to

myXPathNavigator.SetTypedValue("28");

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='http://www.w3.org/1999/XSL/Transform'>

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

</xsl:stylesheet>

"));

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

redchord2.0

ok i went for option 2.

 

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

 

 

myXPathNavigator.MoveToFirstChild();

XmlCDataSection CData = xmlCaseIdTemplate.CreateCDataSection(caseid);

XmlNode node = (XmlNode)myXPathNavigator.UnderlyingObject;

node.ReplaceChild(CData, node.FirstChild);