sulthana
sulthana

Reputation: 299

& is getting converted to & in xml output . How to avoid that in C#?

I am assigning some string value to an attribute of the node which contains & symbol. In the Xml output I am getting ampersand in place of & How can I avoid that?

Here is the code that I have written :

     XmlAttribute attr=doc.CreateAttribute("name");
     attr.Value ="me&mine";
     node.Attributes.Append(attr);
     itemnode.AppendChild(node);
     doc.Save(path);

I am getting output like

     <add name="me&amp;mine"/>

Upvotes: 1

Views: 4744

Answers (2)

Sean
Sean

Reputation: 62532

That's the correct behaviour! If your encoding didn't take place you end up with badly formed xml.

When you load the XML back into your application the &amp; will be converted back to a & character without you having to do anything about it.

Upvotes: 2

Rowland Shaw
Rowland Shaw

Reputation: 38129

That would be the correct output (in fact, it would be invalid as XML if the ampersand wasn't encoded)

Upvotes: 6

Related Questions