2

I have string s and it looks:

<root><p>hello world</p>&nbsp;my name is!</root>

I have next code:

try
{
    m_Content = XDocument.Load(new StringReader(s));
}
catch (XmlException ex)
{
    ex.Data["myerror"] = s;

    throw;
}

As you see, I want to load string with all elements like &nbsp; and make it view. But I've got XmlException:

Reference to undeclared substitution to "nbsp"

Any ideas how to do it right?

Added

ChrisShao offered a good idea: put my string in <![CDATA[ tag, but unfortunately it doesnt solve my problem. I have a big string with lots of tags and few big texts in which I can meet &nbsp; elements. If use System.Web.HttpUtility.HtmlDecode I lose all these elements and get " " fields.

Community
  • 1
  • 1
Matterai
  • 33
  • 1
  • 8

3 Answers3

3

Responding to your Added section. The blank (" ") fields you get is correct representation of &nbsp; when it is rendered. Correct encoding of &nbsp; for use in xml is &#160; [Reference].

If you really want to see &nbsp; instead of " " when the string loaded to XDocument, try to encode ampersand char (&) with &amp;. Replace &nbsp; with &amp;nbsp; [Reference].

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
1

use System.Web.HttpUtility.HtmlDecode or System.Net.WebUtility.HtmlDecode

Vasily Semenov
  • 302
  • 3
  • 6
0

I think you should put your string into CDATA block,like this:

<root><![CDATA[<p>hello world</p>&nbsp;my name is!]]></root>
Chris Shao
  • 8,231
  • 3
  • 39
  • 37
  • But how should I be if my string contains lots of tags and tonns of text where I can meet ` ` elements? – Matterai Feb 18 '14 at 06:48
  • So what's the matter? it can't be parsed correct in CDATA? – Chris Shao Feb 18 '14 at 07:12
  • Yep! I put `<![CDATA[` in the begin of string and `]]>` in the end. When I look on page I see `]]>` in the end of my shown elements. I think you'll agree with me that it shouldn't be there. – Matterai Feb 18 '14 at 07:21