0

Suppose we have this code:

string actualvalue = "<a>";
string abc = "<firstnode><secondnode abc=\""+ actualvalue + "\"></secondnode></firstnode>";

XElement item2 = XElement.Parse(abc);
foreach (var item in item2.Descendants("secondnode"))
{
     string aftergettingitfromXlement = item.Attribute("abc").Value;
     ///so aftergettingitfromXlement  = "<a>" which is not correct i want to have actual value which is encoded 
}

Any idea how can I get the actual value, I don't want to encode again. Why is the encoding lost?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Ahtesham ul haq
  • 319
  • 1
  • 5
  • 14

1 Answers1

0

Try this instead:

string actualvalue = "&amp;lt;a&amp;gt;";

The XDocument implemenation assumes that the value in your xml string is escaped. To read more about escpae characters in xml: What characters do I need to escape in XML documents?

To do a general replace of ampersands in a string you could write:

string actualvalue = "&lt;a&gt;";
actualvalue = actualvalue.Replace("&", "&amp;");
mortb
  • 9,361
  • 3
  • 26
  • 44