473,385 Members | 1,356 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,385 software developers and data experts.

Problem using XmlTextReader and XmlTextWriter.

I'm trying to read an XML document and write out a slightly modified
version using the XmlTextWriter.
I'm basically trying to copy all the nodes exactly as they are read and
do some text manipulation on #text nodes of only certain named elements.

example

while (xmltextreader.Read())
{
if (xmltextreader.NodeType == XmlNodeType.Text)
{
string modified = xmltextreader.Value;
// manipulate string here
xmltextwriter.WriteString(modified);
}
else
{
// The dilemma is here!
xmltextwriter.WriteNode(xmltextreader, false);
}
}

The problem I face is that the WriteNode method of the XmlTextWriter
copies all of its children and this creates a problem for iterating
through all the nodes. I would like some way to pass some nodes through
to the writer with no changes.

Does anyone have a solution for performing this kind of simple
processing?

Thanks,

Pete
Nov 12 '05 #1
3 4174
Pete

You can do switch on NodeType for all types of nodes XmlReader handles. It's not much, only 18 case statements and you will have more granular control of the output xml in future. You can use WriteNode() if you absolutely sure current node doesn't have any children you need to modify like if(reader.Name != "ThisNodeHasSomeSuffToModify") writer.WriteNode(reader, false).

Paul
Nov 12 '05 #2
Thanks Paul,

I wondered if there was a way to do this without a lengthy switch block.
Have you seen any examples of this type of code that has been tested and
ready to use? Just curious. I'm still debating whether it would be more
robust to do this using xslt instead.

In any case, thanks for the suggestion.

Pete

Pete,

You can do switch on NodeType for all types of nodes XmlReader
handles. It's not much, only 18 case statements and you will have more
granular control of the output xml in future. You can use WriteNode()
if you absolutely sure current node doesn't have any children you need
to modify like if(reader.Name != "ThisNodeHasSomeSuffToModify")
writer.WriteNode(reader, false).

Paul


Nov 12 '05 #3
You can create your own XmlWriter based on XmlTextWriter, overrite
WirteNode; do what you have to do and/or call base.WriteNode.
Yan

"Pete" <no****@nospamever.com> wrote in message
news:VFjrc.7235$g71.2254@clgrps13...
Thanks Paul,

I wondered if there was a way to do this without a lengthy switch block.
Have you seen any examples of this type of code that has been tested and
ready to use? Just curious. I'm still debating whether it would be more
robust to do this using xslt instead.

In any case, thanks for the suggestion.

Pete

Pete,

You can do switch on NodeType for all types of nodes XmlReader
handles. It's not much, only 18 case statements and you will have more
granular control of the output xml in future. You can use WriteNode()
if you absolutely sure current node doesn't have any children you need
to modify like if(reader.Name != "ThisNodeHasSomeSuffToModify")
writer.WriteNode(reader, false).

Paul

Nov 12 '05 #4

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

Similar topics

1
by: André Gasser | last post by:
hello newsgroup, I just discovered a weird effect in my php code. here is the flow of my code: 1. upload jepg file to server 2. create new (empty) jpeg file using imagecreatefromjpeg()...
3
by: prasad | last post by:
Hi, I am using XMLTextReader class to read the xml files. In some cases xml declaration tag might start after space/tab charecters. These kind of files are supported by the browsers and xml dom's...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
0
by: kp | last post by:
Hi, I'm using XMLTextReader to read from a file. As I loop through the reader, I check a condition (see below). While reader.Read() If (reader.Name = "Test" And (reader.NodeType =...
3
by: rrm | last post by:
Hi I am verifying a SLD implementation using xml, however I have som problem using xpath and default namespaces in vb.net Currently I have the following xml <?xml version="1.0"?>...
3
by: Franky | last post by:
Been having a problem using a treeview. Work great the first time the form containing it is entered but not the second time. Took a while to create a small sample that exhibits the problem, but...
3
by: Goran Djuranovic | last post by:
Hi all, I ran into a problem where my XMLTextReader fails on .Read() when I have "<" character in one of the attribute's values. What I am trying to do is replace illegal characters ("<", "&" ,...
0
by: sena87 | last post by:
As the title says, can anyone tell me how to get the number of child nodes for lets say <script> node in the xml below using XmlTextReader? (which should be 2 - row1 and row2 ) ...
3
by: presencia | last post by:
Hi all, I have a problem using gdb. I am using Eclipse-CDT as a frontend, but since this problem is obviously related to the gcc compiler or the gdb itself, I have reproduced it without Eclipse. ...
1
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: 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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.