2

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.

user642378
  • 167
  • 6
  • 12

3 Answers3

1

where i am getting error in fieldElement.InnerText = ...

I strongly suspect that's not where you're getting the error. Given the part which says "cannot be included in a name" I suspect this is the problem:

fieldElement = doc.CreateElement(itemname);

It's fine to have arbitrary text as text, but not as an element name. it's also unusual to want to create an element name from data. Are you sure you don't want something like:

fieldElement = doc.CreateElement("itemname");

?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I am receiving itemname="Adam & Eva frisør".. This string must be created as xmlelemnt.here i am getting error. – user642378 Jan 25 '13 at 11:36
1

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.

The element tags are case-sensitive; the beginning and end tags must match exactly. Tag names cannot contain any of the characters !"#$%&'()*+,/;<=>?@[]^`{|}~, nor a space character, and cannot start with -, ., or a numeric digit.

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.

fieldElement.InnerText = System.Web.HttpUtility.HtmlEncode(ds.Tables[0].Rows[j]["count"].ToString());
fieldElement1.AppendChild(fieldElement);
Kami
  • 19,134
  • 4
  • 51
  • 63
  • I am receiving itemname="Adam & Eva frisør".. This string must be created as xmlelemnt.here i am getting error. – user642378 Jan 25 '13 at 11:40
  • 1
    Your issue may be the first item I described. You are using invalid characters in the `itemname`. You cannot have spaces, or `&`. Not certain about the special character `ø` but it is a Unicode characters and should be acceptable. Replace or remove the special characters from the itemname, or rework the xml so that the value is an attribute value or content of a node. – Kami Jan 25 '13 at 12:04
0

& needs to be escaped as &amp; otherwise it is invalid XML.

You should XML encode your database text before putting it into the element. See this question on how to do this.

Community
  • 1
  • 1
Paul Ruane
  • 37,459
  • 12
  • 63
  • 82