471,352 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

XMLTextReader - Issue with special characters &,<,>

RJN
Hi

I'm using XMLTextReader to parse the contents of XML. I have issues when
the xml content itself has some special characters like & ,> etc.

<CompanyName>Johnson & Jhonson</CompanyName>
<EmployeeStrength>> 1000</EmployeeStrength>

When I do a Xmltextreader.read() and then check the contents of the xml
node by XmltextReader.ReadString(), I get an exception when I have
special characters. How do I handle this?

Thanks

rjn


*** Sent via Developersdex http://www.developersdex.com ***
Nov 12 '05 #1
1 5192


RJN wrote:

I'm using XMLTextReader to parse the contents of XML. I have issues when
the xml content itself has some special characters like & ,> etc.

<CompanyName>Johnson & Jhonson</CompanyName>
That is not XML then, in XML you need to escape the ampersand as &amp;
(& a m p ;) if it is not the start of a character or entity reference.
<EmployeeStrength>> 1000</EmployeeStrength>


That should not be a problem, a less than '<' has to be escaped as &lt;
but '<' should work and works for me:

const string exampleXML =
"<EmployeeStrength>> 1000</EmployeeStrength>";
XmlTextReader xmlTextReader = new XmlTextReader(new
StringReader(exampleXML));
while (xmlTextReader.Read()) {
if (xmlTextReader.NodeType == XmlNodeType.Element &&
xmlTextReader.Name == "EmployeeStrength")
{
Console.WriteLine("EmployeeStrength content: {0}.",
xmlTextReader.ReadElementString());
}
}
xmlTextReader.Close();

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Christian Schmidbauer | last post: by
2 posts views Thread by Donald Firesmith | last post: by
3 posts views Thread by Jens Kristensen | last post: by
1 post views Thread by Nergock | last post: by

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.