Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1046773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:11:55+00:00 2026-05-16T16:11:55+00:00

I have some XML that looks like this: <abc x={></abc> I want to force

  • 0

I have some XML that looks like this:

<abc x="{"></abc>

I want to force XmlDocument to use the XML character entities of the brackets, ie:

<abc x="&#123;"></abc>

MSDN says this:

In order to assign an attribute value
that contains entity references, the
user must create an XmlAttribute node
plus any XmlText and
XmlEntityReference nodes, build the
appropriate subtree and use
SetAttributeNode to assign it as the
value of an attribute.

CreateEntityReference sounded promising, so I tried this:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<abc />");
XmlAttribute x = doc.CreateAttribute("x");
x.AppendChild(doc.CreateEntityReference("#123"));
doc.DocumentElement.Attributes.Append(x);

And I get the exception Cannot create an 'EntityReference' node with a name starting with '#'.

Any reason why CreateEntityReference doesn’t like the ‘#’ – and more importantly how can I get the character entity into XmlDocument’s XML? Is it even possible? I’m hoping to avoid string manipulation of the OuterXml…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:11:56+00:00Added an answer on May 16, 2026 at 4:11 pm

    You’re mostly out of luck.

    First off, what you’re dealing with are called Character References, which is why CreateEntityReference fails. The sole reason for a character reference to exist is to provide access to characters that would be illegal in a given context or otherwise difficult to create.

    Definition: A character reference
    refers to a specific character in the
    ISO/IEC 10646 character set, for
    example one not directly accessible
    from available input devices.

    (See section 4.1 of the XML spec)

    When an XML processor encounters a character reference, if it is referenced in the value of an attribute (that is, if the &#xxx format is used inside an attribute), it is set to "Included" which means its value is looked up and the text is replaced.

    The string "AT&amp;T;" expands to "
    AT&T;" and the remaining ampersand is
    not recognized as an entity-reference
    delimiter

    (See section 4.4 of the XML spec)

    This is baked into the XML spec and the Microsoft XML stack is doing what it’s required to do: process character references.

    The best I can see you doing is to take a peek at these old XML.com articles, one of which uses XSL to disable output escaping so &amp;#123; would turn into &#123; in the output.
    http://www.xml.com/pub/a/2001/03/14/trxml10.html

    <!DOCTYPE stylesheet [
    <!ENTITY ntilde 
    "<xsl:text disable-output-escaping='yes'>&amp;ntilde;</xsl:text>">
    ]>
    
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         version="1.0">
    
      <xsl:output doctype-system="testOut.dtd"/>
    
      <xsl:template match="test">
        <testOut>
          The Spanish word for "Spain" is "Espa&ntilde;a".
          <xsl:apply-templates/>
        </testOut>
      </xsl:template>
    
    </xsl:stylesheet>
    

    And this one which uses XSL to convert specific character references into other text sequences (to accomplish the same goal as the previous link).
    http://www.xml.com/lpt/a/1426

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    version="2.0">
    
      <xsl:output use-character-maps="cm1"/>
    
      <xsl:character-map name="cm1">
        <xsl:output-character character="&#160;" string="&amp;nbsp;"/>   
        <xsl:output-character character="&#233;" string="&amp;233;"/> <!-- é -->
        <xsl:output-character character="ô" string="&amp;#244;"/>
        <xsl:output-character character="&#8212;" string="--"/>
      </xsl:character-map>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data in an XML element that looks like this: <?xml version=1.0
I have some xml that looks like this... <tbody> <tr> <td> <h5> <a class=foo
Grettings! I have some XML that looks like this: <Root> <SectionA> <Item id=111> <Options>
I have some data in an XML element that looks like this: <item value=category1,category2>Item
I have some xml that looks like this: <?xml version=1.0?> <data> <items> <item><timestamp>2011-07-11T09:01:42Z</timestamp><title><![CDATA[ some
I have some xml that looks like this: <rootElement attribute=' > '/> This is
I have some xml that looks like this: <xml><name>oscar</name><race>puppet</race><class>grouch</class></xml> The tags change and are
I have a basic xml file that looks like this. <root> <item> <title><p>some title</p></title>
I have some XML that looks like this: <Body> <RESULT> <SUCCESS>true</SUCCESS> <SESSIONID>42</SESSIONID> <SESSION_ENCODING>;jsessionid=42</SESSION_ENCODING> </RESULT>
I have some XML that looks something like this: <Root> <Documents> <Document id=1/> </Documents>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.