XmlException - inserting attribute gives "unexpected token" exception

Posted by Anders Svensson on Stack Overflow See other posts from Stack Overflow or by Anders Svensson
Published on 2009-09-25T08:32:58Z Indexed on 2010/05/18 12:41 UTC
Read the original article Hit count: 403

Filed under:
|

Hi,

I have an XmlDocument object in C# that I transform, using XslTransform, to html. In the stylesheet I insert an id attribute in a span tag, taking the id from an element in the XmlDocument. Here is the template for the element:

<xsl:template match="word">
    <span>
        <xsl:attribute name="id"><xsl:value-of select="@id"></xsl:value-of></xsl:attribute>
        <xsl:apply-templates/>
    </span>
</xsl:template>

But then I want to process the result document as an xhtml document (using the XmlDocument dom). So I'm taking a selected element in the html, creating a range out of it, and try to load the element using XmlLoad():

wordElem.LoadXml(range.htmlText);

But this gives me the following exception: "'598' is an unexpected token. The expected token is '"' or '''. Line 1, position 10."

And if I move the cursor over the range.htmlText, I see the tags for the element, and the "id" shows without quotes, which confuses me (i.e.SPAN id=598 instead of SPAN id="598"). To confuse the matter further, if I insert a blank space or something like that in the value of the id in the stylesheet, it works fine, i.e.:

    <span>
        <xsl:attribute name="id"><xsl:text> </xsl:text> <xsl:value-of select="@id"></xsl:value-of></xsl:attribute>
        <xsl:apply-templates/>
    </span>

(Notice the whitespace in the xsl:text element). Now if I move the cursor over the range.htmlText, I see an id with quotes as usual in attributes (and as it shows if I open the html file in notepad or something).

What is going on here? Why can't I insert an attribute this way and have a result that is acceptable as xhtml for XmlDocument to read? I feel I am missing something fundamental, but all this surprises me, since I do this sort of transformations using xsl:attribute to insert attributes all the time for other types of xsl transformations. Why doesn't XmlDocument accept this value?

By the way, it doesn't matter if it is an id attribute. i have tried with the "class" attribute, "style" etc, and also using literal values such as "style" and setting the value to "color:red" and so on. The compiler always complains it is an unvalid token, and does not include quotes for the value unless there is a whitespace or something else in there (linebreaks etc.).

I hope I have provided enough information. Any help will be greatly appreciated.

Basically, what I want to accomplish is set an id in a span element in html, select a word in a webbrowser control with this document loaded, and get the id attribute out of the selected element. I've accomplished everything, and can actually do what I want, but only if I use regex e.g. to get the attribute value, and I want to be able to use XmlDocument instead to simply get the value out of the attribute directly.

I'm sure I'm missing something simple, but if so please tell me.

Regards,

Anders

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml