469,282 Members | 1,779 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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 6074
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by David List | last post: by
3 posts views Thread by DC Gringo | last post: by
8 posts views Thread by shyam | last post: by
3 posts views Thread by Nathan Sokalski | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.