1

In my application I'm using XAttribute.SetValue(...) to set the value of an xml-attribute. The value is provided by the user.

I have the following methode to set the attribute-value:

private void SetValue(XAttribute attribute, string input)
{
    attribute.SetValue(input);
}

If the input is Hello "World" then the value of the attribute is: Hello "World"

This is what I've expected.

If the input is already "masked" like Hello "World" then the value of the attribute is: Hello "World"

That's not correct.

Is there a way to avoid this conversion?

Tomtom
  • 9,087
  • 7
  • 52
  • 95
  • You should of removed the double quotes from input before call the method. You can remove the double quotes simply by using replace : input = input.Replace("\"", ""); – jdweng Aug 19 '19 at 09:56
  • 1
    I don't think you can avoid it, but you can make sure the input is not already encoded with something like `System.Net.WebUtility.HtmlDecode(input)` before you call `SetValue` – steve16351 Aug 19 '19 at 10:08
  • Thanks. `System.Net.WebUtility.HtmlDecode(input)` solved the problem. Post it as answer, so I can accept it – Tomtom Aug 19 '19 at 12:03

1 Answers1

2

You can not stop SetValue encoding characters such as & to entity codes. So, to avoid them being double encoded, you can make sure your input is not encoded to start with. You can do this using:

System.Net.WebUtility.HtmlDocode(input)

Or, if you have a reference to System.Web already,

System.Web.HttpUtility.HtmlDecode(input)

steve16351
  • 5,372
  • 2
  • 16
  • 29