tags:

views:

51

answers:

3

Hi, I am using XMLwriter to create HTML which works well, but now I need to place image tag but I am not able to do that - it still reports I cannot use ' ' in the element name. Please advice hwo to solve that, thanks!

EDIT: Basically, how to create in XML document this:

<img src="path" />
+1  A: 

Well it's absolutely right - you can't have a space in an XML element name, or indeed in an HTML element name. Why do you think you need it? Image tags don't have spaces in the element name...

Are you sure you don't want to put the space in an attribute value?

Could you give us an example?

EDIT: Okay, so in your comment you want something like:

<img src="path" />

Here:

  • img is the element name
  • src is an attribute name
  • path is an attribute value

So you'd use something like:

writer.WriteStartElement("img");
writer.WriteAttributeString("src", "path");
// Any extra bits you wanted
writer.WriteEndElement();
Jon Skeet
I am bit confused, working whole night and day on it :)I will need <img scr="path">
Petr
+1  A: 

It's right, you can't have a space in an XML element name.

In the case of <img src="path" /> the element name is img, then there's an attribute with a name of src and a value of path. So you don't need a space in your element name, you just need to render the rest of it as an attribute rather than trying to put it all in the element name field.

Tim Schneider
Thanks! That was what I could not figure out.
Petr
A: 

An element name must follow very strict rules. It must follow the less-then sign < (after which NO space may occur). Then the first character cannot be a dash, a dot or a number (and some others). Finally the rest of the name follows which may include dashes or dots.

The name is further restricted for XML + Namespaces.

Abel