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

XmlTextReader and XmlTextWriter

Hi - I am trying to read an XML file using XmlTextReader and write it
out using XmlTextWriter.

The XML is XHTML. At the moment, I am trying to create a straightforward
copy of a file. The XHTML file contains nodes and attributes (with there
own namespace) that will later be used to alter the XML into a desired
output. This 'copying' is the first stage to then processing the XML to
the desired output

I have included the code below. If I put debug stops on in the
appropriate place, the reader is correctly and moving through the XML.

However, the XML writing is incorrect. It seems as though if an xml
element contains an empty element - e.g. <n1><nd/></n1>, the </n1>
appears at the end of all the following elements in the XML document.

I am little confused as to which methods of write that I should be using
when I read elements from the read. I reasonably understand using the
writer to create 'my own xml', it's in this example when I require to
write what I read.

Many thanks for any help

while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
String name = reader.Name;
NameValueCollection attributes = new NameValueCollection();
while (reader.MoveToNextAttribute())
{
String attributeName = reader.Name;
String attributeValue = reader.Value;
attributes.Add(attributeName, attributeValue);
}
writer.WriteStartElement(name);
foreach (String attributeName in attributes)
{
writer.WriteAttributeString(attributeName, attributes[attributeName]);
}
break;
case XmlNodeType.EndElement:
writer.WriteEndElement();
break;
case XmlNodeType.Whitespace:
writer.WriteWhitespace(reader.Value);
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.DocumentType:
break;
default:
break;
}
}
Nov 12 '05 #1
1 2857
Problem solved:

After spending a few hours searching for the answer and writing this, I
found a useful message 5 mins later on usenet that pointed to the
problem - I need to MoveToElement() after reading the attributes and
then I can test for IsEmptyElement.

Regards

Lee

Lee Atkinson wrote:
Hi - I am trying to read an XML file using XmlTextReader and write it
out using XmlTextWriter.

The XML is XHTML. At the moment, I am trying to create a straightforward
copy of a file. The XHTML file contains nodes and attributes (with there
own namespace) that will later be used to alter the XML into a desired
output. This 'copying' is the first stage to then processing the XML to
the desired output

I have included the code below. If I put debug stops on in the
appropriate place, the reader is correctly and moving through the XML.

However, the XML writing is incorrect. It seems as though if an xml
element contains an empty element - e.g. <n1><nd/></n1>, the </n1>
appears at the end of all the following elements in the XML document.

I am little confused as to which methods of write that I should be using
when I read elements from the read. I reasonably understand using the
writer to create 'my own xml', it's in this example when I require to
write what I read.

Many thanks for any help

while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
String name = reader.Name;
NameValueCollection attributes = new NameValueCollection();
while (reader.MoveToNextAttribute())
{
String attributeName = reader.Name;
String attributeValue = reader.Value;
attributes.Add(attributeName, attributeValue);
}
writer.WriteStartElement(name);
foreach (String attributeName in attributes)
{
writer.WriteAttributeString(attributeName, attributes[attributeName]);
}
break;
case XmlNodeType.EndElement:
writer.WriteEndElement();
break;
case XmlNodeType.Whitespace:
writer.WriteWhitespace(reader.Value);
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.DocumentType:
break;
default:
break;
}
}

Nov 12 '05 #2

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

Similar topics

3
by: Russell Mangel | last post by:
Is it possible to use the using statement with XmlTextReader? I tryed to use it, but it gives me the error message: Cannot implicitly convert type 'System.Xml.XmlTextReader' to...
3
by: Pete | last post by:
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...
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
2
by: decrypted | last post by:
I wrote some code to save layout of forms in my application to an XML File. One of the forms has a control which can emit its own layout in the form of XML....so for this controls, I obviously just...
1
by: Borgbjerg | last post by:
I've got a multi-threaded server (we are very early in the project, trying to uncover some risks), and I want to send data using XML. The class ServerAdapter is supposed to connect to the server...
17
by: Slonocode | last post by:
Is there any relation between the NodeType and the Text when using the XmlTextReader? The documentation uses select/case while using the xmltextreader. While reader.Read() Select Case...
1
by: SAM DLG | last post by:
I'm not sure how to do this, but in my n-tiered app, i need to pass xmlreaders as parameters as opposed to strings and as opposed to xmldocuments(). here's what I have, I can write fine to a...
13
by: Rick | last post by:
The following code will enter an infinate loop when in ReadChars. I can only make it happen when reading a Stream and with this particular XML. If I use the ReadInnerXml call rather than my own...
3
by: apiringmvp | last post by:
All, So I am creating a function that gets a short blurb of html from a blog. I would like to retain all html formating and images. The code below works well, with the exception of one issue....
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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,...
0
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...
0
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,...

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.