0
Answer

XML namespaces and prefixes

Ask a question
Andy

Andy

15y
2.6k
1
Hi,

I am trying to create a XML document with specific prefixes, something like the following:
  1. <my:doc>
  2.   <book:item property:changed="12/3/2009">Title </book>
  3.   <author:item property:changed="12/3/2009">admin</author>
  4. </doc>
I am creating a node the following way:

                    XmlDocument xmlDom;
                    XmlElement xmlItem;
                    XmlText xmlText;

                    xmlItem = xmlDom.CreateElement("MyTag");
                    XmlAttribute attr = xmlDom.CreateAttribute("attribute");
                    attr.InnerText = "Text";
                    xmlItem.Attributes.Append(attr);

                    xmlText = xmlDom.CreateTextNode("some text");
                    xmlItem.AppendChild(xmlText);

And the result is something like:

<MyTag attribute="Text">some text</MyTag>

I tried to add these prefixes with the xmlDom.CreateAttribute("","",""); method, but this doesn't seem to work. I am also stuck at setting the prefix/namespace of the XML tag (e.g. prefix:MyTag).

Any help would be appreciated... thanks in advance.