473,804 Members | 3,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CDATA and XmlNode

Hello,

I have the following code:

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

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

// Filter the node we want.
XmlNodeList nodes;
nodes = root.SelectNode s("/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 25257
Hi Lesek,

Below is some sample code to acess a CDATA section:

string fileName = "test.xml";
XmlDocument doc = new XmlDocument();
doc.Load(fileNa me);
XmlElement root = doc.DocumentEle ment;
XmlNode node = doc.DocumentEle ment.SelectSing leNode(
@"/Computers/Computer[@Manufacturer=' Gateway']/CPU");
XmlNode childNode = node.ChildNodes[0];
if (childNode is XmlCDataSection )
{
XmlCDataSection cdataSection = childNode as XmlCDataSection ;
Console.WriteLi ne(cdataSection .Value);
}

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

I hope this helps,

Bennie Haelen

"Leszek" <ta******@5thbu siness.com> wrote in message
news:OK******** ******@TK2MSFTN GP09.phx.gbl...
Hello,

I have the following code:

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

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

// Filter the node we want.
XmlNodeList nodes;
nodes = root.SelectNode s("/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*****@bellso uth.net> wrote in message
news:eM******** ******@TK2MSFTN GP10.phx.gbl...
Hi Lesek,

Below is some sample code to acess a CDATA section:

string fileName = "test.xml";
XmlDocument doc = new XmlDocument();
doc.Load(fileNa me);
XmlElement root = doc.DocumentEle ment;
XmlNode node = doc.DocumentEle ment.SelectSing leNode(
@"/Computers/Computer[@Manufacturer=' Gateway']/CPU");
XmlNode childNode = node.ChildNodes[0];
if (childNode is XmlCDataSection )
{
XmlCDataSection cdataSection = childNode as XmlCDataSection ;
Console.WriteLi ne(cdataSection .Value);
}

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

I hope this helps,

Bennie Haelen

"Leszek" <ta******@5thbu siness.com> wrote in message
news:OK******** ******@TK2MSFTN GP09.phx.gbl...
Hello,

I have the following code:

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

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

// Filter the node we want.
XmlNodeList nodes;
nodes = root.SelectNode s("/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
12001
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 XmlDocument doc.Load("C:\Projects\SQLXML\corc.xml") I get the following error: "System.Xml.XmlException: An unexpected end of file parsing CDATA has
3
8121
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 run the class it gives an error that "System.Xml.XmlNode.XmlNode() is inaccessible due to its protection level". This error comes because XmlNode has not any public constructor. I found XmlNode has two constructor but both are private or friend...
4
5892
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> <age> 28 </age>
2
9743
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 Anything;
2
1494
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 values by XPath having an XmlNode representing the <offer> element. Now when I try something like this
0
1309
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> <FIELD> <DATA>c:\myfile.dat</DATA> </FIELD>
4
2345
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> <node2>a>b</node2>
1
7605
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 void Button2_Click(object sender, EventArgs e)
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.