How do I prefix attributes with a namespace?

S

Sam-I-Am

I need to create xml like the following:
<item mynamespace:name="MyName" mynamespace:age="100" />

I am using code similar to this:
XmlDocument meegoInfo = _LoadTemplateXml("Avatar");

// Create downlevel item
XmlNode node= xmlDoc.DocumentElement;
XmlElement item = xmlDoc.CreateElement("item");
item.SetAttribute("name", "MyName");
item.SetAttribute("age", "100");
node.AppendChild(item);

Cheers,

Sam-I-Am
 
D

Dennis Myrén

Then you will need to use XmlAttribute rather than SetAttribute.
You will need to associate an URI with the namespace prefix.

XmlAttribute a = [XmlDocument].CreateAttribute
("mynamespace", "name", "http://ourcompany.com/mynamespace/");
[XmlNode].Attributes.Append(a);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top