Search code examples
c#xmllinq-to-xmlxattribute

XAttribute.SetValue converts "


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?


Solution

  • 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)