0

Hi,
I am writing some data to a particular xml file .There is one element for which the attribute value contains & ,
eg :

XmlAttribute value=doc.CreateAttribute("Value");
Element1.SetAttributeNode(Value1);
Value1.Value=""";

Output: <Element1 Value="&amp;quot;">

What is the escape sequence for printing an & in an xml file ??

Edited by __avd: Improve the formatting

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by lena20
0

When you are setting the value of the attribute, you don't have to worry about the xml escape sequence, just the c# escape sequence \". Try the following code:

var doc = new XmlDocument();

var rootElement = doc.CreateElement("root");

doc.AppendChild(rootElement);

var elementWithAttribute = doc.CreateElement("child");

var valueAttribute = doc.CreateAttribute("value");

valueAttribute.Value = "&\"Hello World\"";

elementWithAttribute.Attributes.Append(valueAttribute);

rootElement.AppendChild(elementWithAttribute);

var xmlStr = doc.InnerXml;

// at this point, the value of xml string is: 
// <root><child value="&amp;&quot;Hello World&quot;" /></root>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.