By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,190 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

XMLTextReader - Issue with special characters &,<,>

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a


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.