4

XmlElement child = doc.CreateElement(element);

Where doc is an object of XmlDocument. When the code executes the above line with element=Tom and Jerry, I get the following error:

The ' ' character, hexadecimal value 0x20, cannot be included in a name.

What should I do to include ' ' in XmlDocument? I can not replace it with anything else.

What are the other characters which XML element does not support for name ?

Ed Schwehm
  • 2,155
  • 3
  • 32
  • 55
Rauf
  • 11,766
  • 19
  • 71
  • 123

3 Answers3

6

I suppose you want an element with the value "Tom and Jerry", which is fine.

It is part of the XML syntax that you cannot have a space in the name of an element or attribute.

A possible method:

XmlElement child = doc.CreateElement("cartoon");
child.InnerText = "Tom and Jerry";

which produces

<cartoon>Tom and Jerry</cartoon>

Aside, consider XDocument when you can. Much easier than XmlDocument.

XElement child = new XElement("cartoon", "Tom and Jerry");
Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
4

It seems your XML element name contains spaces...

This is illegal:

<tom and jerry>hi</tom and jerry>

Must be this:

<tomandjerry>hi</tomandjerry>
Aliostad
  • 78,844
  • 21
  • 155
  • 205
0

U can make the element like this:

<element name='Tom and Jerry' />

But if you need to store some data for this cartoon and have access to it by cartoon name:

<element name='Tom and Jerry'>some data</element>
Deitro
  • 257
  • 2
  • 10