Search code examples
c#asp.netxelement

Allow special characters in Xelement.SetAttributeValue


I want to use special characters in the name of the attribute like ":" how can i do that?

.SetAttributeValue("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

when i try it says that ":" is not allowed as a attribute name


Solution

  • You can do it like this:

    el.SetAttributeValue(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance");
    

    Alternative syntax:

    el.SetAttributeValue(XNamespace.Xmlns.GetName("xsi"), "http://www.w3.org/2001/XMLSchema-instance");