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

Cannot use & as an attribute value

Hi,
I have a standalone XML file (with the appropriate xml document header)
that works fine when I load it using XmlDocument. I can have child elements
like this without problems:

<SomeChildElement type="c" href="There.aspx?id=5" />

But as soon as I change it to something like this:

<SomeChildElement type="c" href="There.aspx?id=5&amp;t=m" />

I get an XML Exception indicating that it expected quote (or something like
that). I thought the &amp; was an internatl entity that could always be
used. Or can it only be used as part of an InnerText but not an Attribute?
How can I resolve that problem?

Thanks,
Emilio
Nov 12 '05 #1
12 6276
This is the error I get:

Error While loading XML System.Xml.XmlException: This is an unexpected
token. The expected token is 'SEMICOLON'. Line 14, position 32. at
System.Xml.XmlScanner.ScanNamedEntity() at
System.Xml.XmlTextReader.ExpandLiteral(XmlAttribut eTokenInfo currToken) at
System.Xml.XmlTextReader.SetLiteralValues(XmlAttri buteTokenInfo fld) at
System.Xml.XmlTextReader.SetAttributeValues() at
System.Xml.XmlTextReader.ParseElement() at System.Xml.XmlTextReader.Read()
at System.Xml.XmlValidatingReader.ReadNoCollectTextTo ken() at
System.Xml.XmlValidatingReader.Read() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadChildren(XmlNode parent) at
System.Xml.XmlLoader.LoadElementNode() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadChildren(XmlNode parent) at
System.Xml.XmlLoader.LoadElementNode() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) at
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean
preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at
System.Xml.XmlDocument.LoadXml(String xml) at
"~~~ .NET Ed ~~~" <ti*********@abolishspam.now> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,
I have a standalone XML file (with the appropriate xml document header)
that works fine when I load it using XmlDocument. I can have child
elements like this without problems:

<SomeChildElement type="c" href="There.aspx?id=5" />

But as soon as I change it to something like this:

<SomeChildElement type="c" href="There.aspx?id=5&amp;t=m" />

I get an XML Exception indicating that it expected quote (or something
like that). I thought the &amp; was an internatl entity that could always
be used. Or can it only be used as part of an InnerText but not an
Attribute?
How can I resolve that problem?

Thanks,
Emilio

Nov 12 '05 #2
~~~ .NET Ed ~~~ wrote:
This is the error I get:

Error While loading XML System.Xml.XmlException: This is an unexpected
token. The expected token is 'SEMICOLON'. Line 14, position 32. at
System.Xml.XmlScanner.ScanNamedEntity() at
System.Xml.XmlTextReader.ExpandLiteral(XmlAttribut eTokenInfo currToken) at
System.Xml.XmlTextReader.SetLiteralValues(XmlAttri buteTokenInfo fld) at
System.Xml.XmlTextReader.SetAttributeValues() at
System.Xml.XmlTextReader.ParseElement() at System.Xml.XmlTextReader.Read()
at System.Xml.XmlValidatingReader.ReadNoCollectTextTo ken() at
System.Xml.XmlValidatingReader.Read() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadChildren(XmlNode parent) at
System.Xml.XmlLoader.LoadElementNode() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadChildren(XmlNode parent) at
System.Xml.XmlLoader.LoadElementNode() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) at
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean
preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at
System.Xml.XmlDocument.LoadXml(String xml) at
"~~~ .NET Ed ~~~" <ti*********@abolishspam.now> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,
I have a standalone XML file (with the appropriate xml document header)
that works fine when I load it using XmlDocument. I can have child
elements like this without problems:

<SomeChildElement type="c" href="There.aspx?id=5" />

But as soon as I change it to something like this:

<SomeChildElement type="c" href="There.aspx?id=5&amp;t=m" />

I get an XML Exception indicating that it expected quote (or something
like that). I thought the &amp; was an internatl entity that could always
be used. Or can it only be used as part of an InnerText but not an
Attribute?
How can I resolve that problem?

Thanks,
Emilio

Use hexadecimal %26 instead.
Nov 12 '05 #3
~~~ .NET Ed ~~~ wrote:
I have a standalone XML file (with the appropriate xml document header)
that works fine when I load it using XmlDocument. I can have child elements
like this without problems:

<SomeChildElement type="c" href="There.aspx?id=5" />

But as soon as I change it to something like this:

<SomeChildElement type="c" href="There.aspx?id=5&amp;t=m" />

I get an XML Exception indicating that it expected quote (or something like
that). I thought the &amp; was an internatl entity that could always be
used. Or can it only be used as part of an InnerText but not an Attribute?
How can I resolve that problem?


That's weird. I can't reproduce the problem. What is at Line 14,
position 32 ? Provide more info anyway.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4
The info is exactly as provided, but let's put it all together. My input
file, that is being loaded looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<ContentBoxes>
<Container id="Literal1">
<add href="There.aspx" />
<add href="That.aspx" />
<add href="There1.aspx" />
<add href="That1.aspx" />
<add href="That1.aspx" />
<add href="JumpTo.aspx?r=0&t=m" /> <========= Offending
Line
</Container>
</ContentBoxes>

As shown from my original file, the 6th "add" child element is the offending
one, but then ONLY when I add
the &t=m if I leave it out "JumpTo.aspx?r=0" then no error is produced.

As for what is at line X position Y, it doesn't matter because every time I
get that error, the line & Position the
Xml reports to me does not match with the actual file. BUT I know THAT is
the offending line.

Test Case #1: JumpTo.aspx?r=0&t=m ---> the error shown in the follow
up post from me
Test Case #2: JumpTo.aspx?r=0 ---> everything works fine, no
error is produced

Then I suppose the next question is "but HOW do you read this file?", ok,
here is the relevant code snippets:

StreamReader sr = new StreamReader(this.MapPath(filename));
XmlDocument doc = new XmlDocument();
doc.Load(sr); // <=======
Exception occurrs here ONLY with Test Case #1
string xpath = string.Format("descendant::Container[@id='{0}']",
containerID);
XmlNode node = doc.DocumentElement.SelectSingleNode(xpath);
:
sr.Close();
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:eG**************@TK2MSFTNGP15.phx.gbl...
~~~ .NET Ed ~~~ wrote:
I have a standalone XML file (with the appropriate xml document
header) that works fine when I load it using XmlDocument. I can have
child elements like this without problems:

<SomeChildElement type="c" href="There.aspx?id=5" />

But as soon as I change it to something like this:

<SomeChildElement type="c" href="There.aspx?id=5&amp;t=m" />

I get an XML Exception indicating that it expected quote (or something
like that). I thought the &amp; was an internatl entity that could always
be used. Or can it only be used as part of an InnerText but not an
Attribute?
How can I resolve that problem?


That's weird. I can't reproduce the problem. What is at Line 14, position
32 ? Provide more info anyway.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #5
Thanks Nicole for the answer, here is what I found out while using the %26
instead of &amp; but I am afraid it
does not work either. So, if I had "JumpTo.aspx?r=0%26p=1" :

a) The doc.Load(myXMLFile) no longer produces an error, it swallows the %26
without complain
b) The Request.QueryString array shows that only ONE query string parameter
was provided: "r", so
Request.QueryString["r"] = "0%26p=1" as you can see, the %26 is *not*
recognized as a query
string parameter separator.

Quite frankly I cannot conceive WHY a predefined entity such as &amp;
cannot be used in a standalone
(yes I tried with standalone="yes" and got some other error spitted out,
something about it being illegal) XML
file within an XML element's attribute value.

Thanks,
Emilio

"Nicole Schenk" <ns*****@attglobal.net> wrote in message
news:Sr****************@twister.socal.rr.com...
~~~ .NET Ed ~~~ wrote:
This is the error I get:

Error While loading XML System.Xml.XmlException: This is an unexpected
token. The expected token is 'SEMICOLON'. Line 14, position 32. at
System.Xml.XmlScanner.ScanNamedEntity() at
System.Xml.XmlTextReader.ExpandLiteral(XmlAttribut eTokenInfo currToken)
at
System.Xml.XmlTextReader.SetLiteralValues(XmlAttri buteTokenInfo fld) at
System.Xml.XmlTextReader.SetAttributeValues() at
System.Xml.XmlTextReader.ParseElement() at
System.Xml.XmlTextReader.Read()
at System.Xml.XmlValidatingReader.ReadNoCollectTextTo ken() at
System.Xml.XmlValidatingReader.Read() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadChildren(XmlNode parent) at
System.Xml.XmlLoader.LoadElementNode() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadChildren(XmlNode parent) at
System.Xml.XmlLoader.LoadElementNode() at
System.Xml.XmlLoader.LoadCurrentNode() at
System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) at
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean
preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at
System.Xml.XmlDocument.LoadXml(String xml) at
"~~~ .NET Ed ~~~" <ti*********@abolishspam.now> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,
I have a standalone XML file (with the appropriate xml document
header)
that works fine when I load it using XmlDocument. I can have child
elements like this without problems:

<SomeChildElement type="c" href="There.aspx?id=5" />

But as soon as I change it to something like this:

<SomeChildElement type="c" href="There.aspx?id=5&amp;t=m" />

I get an XML Exception indicating that it expected quote (or something
like that). I thought the &amp; was an internatl entity that could
always
be used. Or can it only be used as part of an InnerText but not an
Attribute?
How can I resolve that problem?

Thanks,
Emilio

Use hexadecimal %26 instead.

Nov 12 '05 #6


~~~ .NET Ed ~~~ wrote:

<add href="JumpTo.aspx?r=0&t=m" /> <========= Offending
Line

You need to escape the ampersand e.g.
<add href="JumpTo.aspx?r=0&amp;t=m" />

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #7
I think during the editing of the follow up the thing got confused. Escaping
the ampersand actually *causes* the exception. See the original post. So,
using &amp; does not work, at least not when used in an element's attribute.

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:eV**************@TK2MSFTNGP14.phx.gbl...


~~~ .NET Ed ~~~ wrote:

<add href="JumpTo.aspx?r=0&t=m" /> <========= Offending
Line

You need to escape the ampersand e.g.
<add href="JumpTo.aspx?r=0&amp;t=m" />

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #8
~~~ .NET Ed ~~~ wrote:
I think during the editing of the follow up the thing got confused. Escaping
the ampersand actually *causes* the exception. See the original post. So,
using &amp; does not work, at least not when used in an element's attribute.


That's weird. It works and always worked for me. Can you put sample
docuemnt somewhere on the net?

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #9
Here is the sample document

<?xml version="1.0" encoding="utf-8" ?>
<ContentBoxes>
<Container id="Literal1">
<add href="There.aspx" />
<add href="That.aspx" />
<add href="There1.aspx" />
<add href="That1.aspx" />
<add href="That1.aspx" />
<add href="JumpTo.aspx?r=0&amp;t=m" />
</Container>
</ContentBoxes>

Only make sure the last "add" element (which is the offending element) has
the XML ampersand entity (&amp; and not the ampersand sign, somehow when I
post it gets converted to just the ampersand.
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:up**************@tk2msftngp13.phx.gbl...
~~~ .NET Ed ~~~ wrote:
I think during the editing of the follow up the thing got confused.
Escaping the ampersand actually *causes* the exception. See the original
post. So, using &amp; does not work, at least not when used in an
element's attribute.


That's weird. It works and always worked for me. Can you put sample
docuemnt somewhere on the net?

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #10
~~~ .NET Ed ~~~ wrote:
Here is the sample document

<?xml version="1.0" encoding="utf-8" ?>
<ContentBoxes>
<Container id="Literal1">
<add href="There.aspx" />
<add href="That.aspx" />
<add href="There1.aspx" />
<add href="That1.aspx" />
<add href="That1.aspx" />
<add href="JumpTo.aspx?r=0&amp;t=m" />
</Container>
</ContentBoxes>

Only make sure the last "add" element (which is the offending element) has
the XML ampersand entity (&amp; and not the ampersand sign, somehow when I
post it gets converted to just the ampersand.


Yes, it's & a m p ; Still no problems to load into XmlDocument. Show us
your code.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #11
The code was shown in the original post of 12 dec 13:03 but here it is
again. The exception is produced in the doc.Xml() call, I have used this
code time and time again. The problem is with the document attribute.

StreamReader sr = new StreamReader(this.MapPath(filename));
XmlDocument doc = new XmlDocument();
doc.Load(sr); // <======= Exception
occurrs here ONLY with Test Case #1
string xpath = string.Format("descendant::Container[@id='{0}']",
containerID);
XmlNode node = doc.DocumentElement.SelectSingleNode(xpath);
:
sr.Close();
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:el*************@TK2MSFTNGP11.phx.gbl...
~~~ .NET Ed ~~~ wrote:
Here is the sample document

<?xml version="1.0" encoding="utf-8" ?>
<ContentBoxes>
<Container id="Literal1">
<add href="There.aspx" />
<add href="That.aspx" />
<add href="There1.aspx" />
<add href="That1.aspx" />
<add href="That1.aspx" />
<add href="JumpTo.aspx?r=0&amp;t=m" />
</Container>
</ContentBoxes>

Only make sure the last "add" element (which is the offending element)
has the XML ampersand entity (&amp; and not the ampersand sign, somehow
when I post it gets converted to just the ampersand.


Yes, it's & a m p ; Still no problems to load into XmlDocument. Show us
your code.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #12
~~~ .NET Ed ~~~ wrote:
The code was shown in the original post of 12 dec 13:03 but here it is
again. The exception is produced in the doc.Xml() call, I have used this
code time and time again. The problem is with the document attribute.

StreamReader sr = new StreamReader(this.MapPath(filename));
XmlDocument doc = new XmlDocument();
doc.Load(sr); // <======= Exception
occurrs here ONLY with Test Case #1
string xpath = string.Format("descendant::Container[@id='{0}']",
containerID);
XmlNode node = doc.DocumentElement.SelectSingleNode(xpath);


Works fine for me. How can I reproduce the problem?

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #13

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

Similar topics

12
by: Sammy | last post by:
Hi, my mind is going crazy. I have tried everything I can think of to no avail. I have tried Disable Output Escaping. I tried to think of a way of enclosing the attribute data in a CDATA...
6
by: David List | last post by:
I'm having a problem using different properties of the document object in the example javascripts in my textbook with browsers that identify themselves as using the Mozilla engine. One example of...
3
by: DC Gringo | last post by:
I have an image control (that pulls an image off an ESRI map server): <ASP:IMAGE ID="imgZonedCountry" RUNAT="server"></ASP:IMAGE> In the code behind I am setting the ImageURL to a String value...
8
by: shyam | last post by:
Hi All As per my understanding we use xs:key when we want to have a unique value for a element or attribute and keyref to refer to one of the unique values of that element or attribute. ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
3
by: Nathan Sokalski | last post by:
I am adding an onmouseover attribute using the Attributes.Add() method, and the String I am using for the value contains the & character. However, when rendered the & is converted to the HTML...
2
by: mlb5000 | last post by:
I seem to be having issues validating an XML document using my schema. Both are below: The Schema: <?xml version="1.0" encoding="UTF-8"?> <xs:schema...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.