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

XmlReader problem with &

I have assigned a simple xml into a XmlReader. A switch case loops
through the nodes and writes the data to XmlWriter object which is
initialized to a xml file. The output xml file is correctly generated
and everything works fine. But if the input xml has an & then I am
unable to generate a valid output xml file.

I have pasted the code below:

public class Class1
{
public Class1()
{
}
public static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(@"<FName>First &amp;
Name</FName>");
System.IO.StringReader sr = new
System.IO.StringReader(sb.ToString());
System.Xml.XmlTextReader xtr = new
System.Xml.XmlTextReader(sr);
System.Xml.XmlReader xr = xtr;
System.Xml.XmlTextWriter xtw = new
System.Xml.XmlTextWriter("Output.xml", new System.Text.UTF8Encoding());
System.Xml.XmlWriter xw = xtw;

while (xr.Read())
{
switch (xr.NodeType)
{
case System.Xml.XmlNodeType.Element:
xw.WriteStartElement(xr.LocalName);
break;
case System.Xml.XmlNodeType.EndElement:
xw.WriteEndElement();
break;
case System.Xml.XmlNodeType.Text:
xw.WriteRaw(xr.Value);
break;
default: break;
}
}
xr.Close();
xw.Flush();
xw.Close();
}
}

Please help.

Thanks,
Amit

Aug 8 '06 #1
2 3888
Amit wrote:
I have assigned a simple xml into a XmlReader. A switch case loops
through the nodes and writes the data to XmlWriter object which is
initialized to a xml file. The output xml file is correctly generated
and everything works fine. But if the input xml has an &amp; then I am
unable to generate a valid output xml file.
When you read the value, the &amp; is translated into &. You're then
calling WriteRaw, which is writing out the & as-is - resulting in an
invalid XML file.

If you change the call to WriteRaw to WriteString, it will work fine.

Jon

Aug 8 '06 #2
Thanks .. ...Now it's fine
Jon Skeet [C# MVP] wrote:
Amit wrote:
I have assigned a simple xml into a XmlReader. A switch case loops
through the nodes and writes the data to XmlWriter object which is
initialized to a xml file. The output xml file is correctly generated
and everything works fine. But if the input xml has an &amp; then I am
unable to generate a valid output xml file.

When you read the value, the &amp; is translated into &. You're then
calling WriteRaw, which is writing out the & as-is - resulting in an
invalid XML file.

If you change the call to WriteRaw to WriteString, it will work fine.

Jon
Aug 8 '06 #3

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

Similar topics

2
by: JoRo | last post by:
Hi, I'm trying to add an onclick event to a button control so it will open up a new popup window. Everything works fine except where I add variables to pass on to the popup window. Where I have...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
2
by: Eric Zechman via .NET 247 | last post by:
I have a project in which I get xml posted via a hidden form field (data=<xml></xml>). People are entering names like Bill & Sandy Smith. this is coming over as:...
1
by: Nathan Alden | last post by:
I have an XSD defined as the following: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
4
by: Tom Fields | last post by:
Hello! I like to use the XmlTextWriter to write some SVG files. But in some cases, I need the '&' as '&' and not as &amp;. Example: <glyph unicode="&#x4c;"/> Some code-snippet:
2
by: Jerim | last post by:
Our database stores the & symbol as &amp; I have a select menu that queries the database based on the selection. A sample selection: <option value=\"Cats &amp; Dogs\">Cats & Dogs</option> After I...
9
by: John | last post by:
Hello all. I am a PHP newbie and am having an issue using the && in an if statement. here is the code: if ($_REQUEST == "1" && date("Y-m-d") < $rowWork) { die("<h1>The earlybird special has...
2
by: CindyH | last post by:
Hi I have an xml stream that I would like to read with xmltextreader. Problem is that there are some & inside the xml stream. Parse is not working on them. Can someone show me sample code for...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.