In my c# application i am creating an xml based on the database value. It woks fine until the string is not a special character. Below is my code.
XmlDocument doc = new XmlDocument();
XmlElement element = doc.CreateElement("NewDataSet");
------
string itemname =System.Web.HttpUtility.HtmlEncode(ds.Tables[0].Rows[j]["itemname"].ToString());
fieldElement = doc.CreateElement(itemname);
fieldElement.InnerText = ds.Tables[0].Rows[j]["count"].ToString();
fieldElement1.AppendChild(fieldElement);
where i am getting error in `fieldElement = doc.CreateElement(itemname);
as The ‘ ‘ character, hexadecimal value 0x20, cannot be included in a name.
And the string which throws exception is “Adam & Eva frisør”.
Can anyone tell me how to overcome this problem.
There are two possible issue here. Firstly, you are html encoding the element name. This will work fine until the element name contains a special character.
so, it is an error waiting to happen. Ensure that the name is valid and does not contain any special characters.
Secondly, you are not escaping the content of the element. The text you have quoted contains an unescaped
&. This is causing the actual error you are getting. Use the same escape method you are using for the element name for the element text.