473,387 Members | 2,436 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.

Unexpected Token error while using XmlParserContext

Greetings;

I have an application where I am receiving HTML fragments containing
snippets (sub-fragments?) of XML. I wish to extract the XML bits for further
processing. In the process of playing around with various ways to accomplish
this I came up with the following code:

string xmlFrag=
@"
<P> </P>
<P>This is a test <xml><field name=""instructor-name"">Brian
Cobb</field></xml></P>
<P> </P>
<P>Another test</P><xml><field name=""picture"">brianc.jpg</field></xml>" ;
try
{
string subset = "<!ENTITY nbsp ' '>";
XmlParserContext context = new XmlParserContext(
null,
null,
"html",
#if USING_DOCTYPE_IDS
this.publicID,
#else
null,
#endif
#if USING_DOCTYPE_IDS
this.systemID,
#else
null,
#endif
#if USE_SUBSET
subset,
#else
null,
#endif
"",
"en-us",
XmlSpace.None,
System.Text.Encoding.UTF8
);

XmlValidatingReader reader =
new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
reader.ValidationType = ValidationType.None;
while(reader.Read()) { ...

Note that this.systemID = @"http://www.w3.org/TR/html4/loose.dtd"
and this.publicID = @"-//W3C//DTD HTML 4.01 Transitional//EN"
in my most recent test and that USING_DOCTYPE_IDS is defined.

The problem I am having is, that on the first call to reader.Read() I get an
XmlException with the message: "This is an unexpected token. The expected
token is 'TAGEND'" at Line 31, Column 3". Since there aren't 31 lines in
xmlFrag I would surmise that the problem lies with the external DTD; however,
selecting different values for systemID and publicID produces similar
results. If I #undef USING_DOCTYPE_IDS, the code works as expected.

What I am trying to avoid is having to define my own general entities as I
have done with nbsp in my example. Fiddling with systemID and publicID is
my initial attempt to use standard DTDs to get around this (think of small
child whining "But Mommy, I don't want to define my own DTD"). But, if anyone
has a better idea how to accomplish this, I'm all ears.

thanks.

PS: if USE_SUBSET is #undef'ed I get XmlException "undefined entity nbsp".

Nov 12 '05 #1
1 4767
Brian, even though you have XML fragments embedded in HTML, I don't see much
hope for trying to read the XML out of the HTML using an XML reader because
the HTML is not XML. It doesn't matter what DTD you are using for the HTML,
until you get to XHTML, you can't use XML technologies on HTML.

But what you can to is get an XmlReader than reads HTML. Since HTML is SGML,
you could use the SgmlReader on GotDotNet [1]. Knock yourself out. Go wild.

Cheers,
Stuart Celarier, Fern Creek

[1]
http://www.gotdotnet.com/community/u...ery=sgmlreader
Nov 12 '05 #2

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

Similar topics

2
by: SaCompGeek | last post by:
I have a class that defines a Key/Name value collection that i generated by and XML Element with multiple attributes. The attribute are used to create the collection of 'Binate' objects. This is...
1
by: Najm Hashmi | last post by:
Hi all , I am trying to create a store procedure and I get the following error: SQL0104N An unexpected token "END-OF-STATEMENT" was found following "END". Expected tokens may include: "JOIN...
5
by: r.nikhilk | last post by:
Hi, Currently, we are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by...
0
by: Jen | last post by:
My main question: "How can I get a TextReader or Stream object from an existing XmlReader?". Read on for more: I have an existing XmlReader. Let's call it reader1. I'm creating another reader,...
4
by: lisa | last post by:
I have an XML file that starts like this: <?xml version="1.0" encoding="ISO-8859-1" xmlns:fn="http://www.w3.org/2005/xpath-functions"?> <Authors> <Author> <ID>2</ID>...
1
by: Ben Walling | last post by:
I'm getting the following error on one of 110 machines that is using a webservice to upload database changes to a central location: System.Web.Services Server was unable to read request. --There...
2
by: =?Utf-8?B?Y2FzaGRlc2ttYWM=?= | last post by:
Hi, has anyone come across this error before: This is an unexpected token. The expected token is 'NAME' I am getting it when trying to send an xml file across a web service layer. The...
1
by: FightClubDiego | last post by:
Hey.. I've been working on these sign up / log in forms for my new Game Site, and everything else in the game works but the registration!! I keep ketting the unexpected $end and Im tired of it! Here...
4
by: manontheedge | last post by:
I'm using SSH Secure Shell to connect to a Linux machine ... when I try to run the compiled code I have, I keep getting these errors ... ./p2.c: line 5: syntax error near unexpected token `('...
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:
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: 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
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
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...
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
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.