473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

CDATA and XmlNode

Hello,

I have the following code:

// Read the XML document from the hard-drive.
XmlDocument doc = new XmlDocument();
doc.Load("doc.xml");

// Create the document's root.
XmlElement root = doc.DocumentElement;

// Filter the node we want.
XmlNodeList nodes;
nodes = root.SelectNodes("/Pages/Page[@Filename='" + id + "']");
XmlNode node = nodes[0];

// Retrieve data from the node
m_Filename = id;
m_Caption = node["Caption"].InnerText;
m_Content = node["CDATA"].InnerText; // ??????????????

The problem is that m_Content needs to be populated from CDATA section of
the selected node. There is no <CDATA></CDATA> element so this code fails.

How to do this?

Thanks,
Leszek Taratuta


Nov 12 '05 #1
2 25101
Hi Lesek,

Below is some sample code to acess a CDATA section:

string fileName = "test.xml";
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlElement root = doc.DocumentElement;
XmlNode node = doc.DocumentElement.SelectSingleNode(
@"/Computers/Computer[@Manufacturer='Gateway']/CPU");
XmlNode childNode = node.ChildNodes[0];
if (childNode is XmlCDataSection)
{
XmlCDataSection cdataSection = childNode as XmlCDataSection;
Console.WriteLine(cdataSection.Value);
}

The XML File is shown below:
<Computers>
<Computer Manufacturer="Dell">
<CPU>
Intel Pentium 4
</CPU>
</Computer>
<Computer Manufacturer="Gateway">
<CPU>
<![CDATA[
this is the <CData Text>
]]>
</CPU>
</Computer>
</Computers>

I hope this helps,

Bennie Haelen

"Leszek" <ta******@5thbusiness.com> wrote in message
news:OK**************@TK2MSFTNGP09.phx.gbl...
Hello,

I have the following code:

// Read the XML document from the hard-drive.
XmlDocument doc = new XmlDocument();
doc.Load("doc.xml");

// Create the document's root.
XmlElement root = doc.DocumentElement;

// Filter the node we want.
XmlNodeList nodes;
nodes = root.SelectNodes("/Pages/Page[@Filename='" + id + "']");
XmlNode node = nodes[0];

// Retrieve data from the node
m_Filename = id;
m_Caption = node["Caption"].InnerText;
m_Content = node["CDATA"].InnerText; // ??????????????

The problem is that m_Content needs to be populated from CDATA section of
the selected node. There is no <CDATA></CDATA> element so this code fails.

How to do this?

Thanks,
Leszek Taratuta

Nov 12 '05 #2
Thanks.
Your code works great.

Leszek

<ha*****@bellsouth.net> wrote in message
news:eM**************@TK2MSFTNGP10.phx.gbl...
Hi Lesek,

Below is some sample code to acess a CDATA section:

string fileName = "test.xml";
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlElement root = doc.DocumentElement;
XmlNode node = doc.DocumentElement.SelectSingleNode(
@"/Computers/Computer[@Manufacturer='Gateway']/CPU");
XmlNode childNode = node.ChildNodes[0];
if (childNode is XmlCDataSection)
{
XmlCDataSection cdataSection = childNode as XmlCDataSection;
Console.WriteLine(cdataSection.Value);
}

The XML File is shown below:
<Computers>
<Computer Manufacturer="Dell">
<CPU>
Intel Pentium 4
</CPU>
</Computer>
<Computer Manufacturer="Gateway">
<CPU>
<![CDATA[
this is the <CData Text>
]]>
</CPU>
</Computer>
</Computers>

I hope this helps,

Bennie Haelen

"Leszek" <ta******@5thbusiness.com> wrote in message
news:OK**************@TK2MSFTNGP09.phx.gbl...
Hello,

I have the following code:

// Read the XML document from the hard-drive.
XmlDocument doc = new XmlDocument();
doc.Load("doc.xml");

// Create the document's root.
XmlElement root = doc.DocumentElement;

// Filter the node we want.
XmlNodeList nodes;
nodes = root.SelectNodes("/Pages/Page[@Filename='" + id + "']");
XmlNode node = nodes[0];

// Retrieve data from the node
m_Filename = id;
m_Caption = node["Caption"].InnerText;
m_Content = node["CDATA"].InnerText; // ??????????????

The problem is that m_Content needs to be populated from CDATA section of the selected node. There is no <CDATA></CDATA> element so this code fails.
How to do this?

Thanks,
Leszek Taratuta


Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sunil | last post by:
I am creating a XML document which opens fine in IE. Implies MSXML thinks it is a well formed document. But when I try to load this document in VB.net using the following code Dim doc As New...
3
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
4
by: The Kiddie | last post by:
Hi, I am having major headaches with XmlNode.InsertAfter. This is the format of my XML document <?xml version="1.0" encoding="utf-8"?> <Team name="Team"> <Players> <Player> <name> GK</name>...
2
by: Munish | last post by:
(VS.NET 2K3, C#) Hi, I need to serialize/deserialize an element using XmlSerializer that can contain any valid XML including a CDATA section. public class Test { public XmlElement...
2
by: J Mon | last post by:
I have a XML document with elements like <offer> <field name="name"><!]></field> <field name="merchant_id"><!]></field> ..... </offer> I know not the best XML! Now I am selecting different...
0
by: Philip Wagenaar | last post by:
I have an XML file that will contain the data of a binary file in MIME encoding. My application receiver the XML file with the location of the binary file: <REQUEST> <IMPORT> <DOCUMENT>...
4
by: Stefan Gentzmer | last post by:
Hello, how can i detect, if the text for a XmlElement has to be written as CDATA (i am using the XmlWriter) instead of a normal string. For example <start> <node1>HelloWorld</node1>...
1
by: Dariusz Tomoñ | last post by:
Hi, I have got xml document with CDATA sections containing special characters like links to images. All Iwant is to display the content in my div section. I tried like this: protected...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.