473,396 Members | 1,768 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,396 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 6288
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...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.