all groups > dotnet xml > july 2005 >
You're in the

dotnet xml

group:

Finding & Updating nodes within a CDATA section


Finding & Updating nodes within a CDATA section Paul J Lay
7/25/2005 10:25:23 AM
dotnet xml: BlankI seem to be able to find and update nodes and attributes fairly =
well using the XmlDocument class thanks the great support I received on =
this newgroup. However, I a can't seem to locate properties contained =
with a CDATA section. Is this possible? I have attached a small =
example. Thanks for your help.

Best Regards,

Re: Finding & Updating nodes within a CDATA section Chris Lovett
7/25/2005 8:35:06 PM
BlankThe CDATA section is "hiding" the embedded XML so you will have to =
extract this out. THe following example prints the ipAddress =
"10.0.0.1":
using System;
using System.Text;
using System.Xml;

class Program {
static void Main(string[] args) {
XmlDocument doc =3D new XmlDocument();
doc.Load(@"..\..\xmlfile1.xml");

XmlNode sr =3D doc.SelectSingleNode("//ServiceRequest");

string innerXml =3D sr.InnerText;
doc.LoadXml(innerXml);

XmlNode addr =3D =
doc.SelectSingleNode("/DiscoverRequest/properties/property[@key=3D'ipAddr=
ess']/@value");
Console.WriteLine(addr.InnerText);

}
}

Chris.

[quoted text, click to view]
I seem to be able to find and update nodes and attributes fairly well =
using the XmlDocument class thanks the great support I received on this =
newgroup. However, I a can't seem to locate properties contained with a =
CDATA section. Is this possible? I have attached a small example. =
Thanks for your help.

Best Regards,

RE: Finding & Updating nodes within a CDATA section v-kevy NO[at]SPAM online.microsoft.com
7/26/2005 5:36:13 AM
Hi Paul,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to find and update certain
nodes in an xml document within a CDATA element of another xml document. If
there is any misunderstanding, please feel free to let me know.

In this case, since an Xml document is nesting in another one, we cannot
modify the properties in the CDATA element directly. Because all the data
within this section is regarded as string. So we need to do the following
steps to manipulate on the inner xml document.

1. Load the whole doc into an XmlDocument object.
2. Locate the CDATA element and get its content as a string.
3. Load the string into another XmlDocument object.
4. modify the doc and get the changed string and set it back to the CDATA
element.

Thus, the whole doc is updated. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
AddThis Social Bookmark Button