Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

White space in XmlElement in C# .Net

Tags:

c#

xml

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 ?

like image 611
Rauf Avatar asked Dec 27 '22 10:12

Rauf


1 Answers

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");
like image 56
Henk Holterman Avatar answered Jan 10 '23 10:01

Henk Holterman