Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add xml attributes with different prefixes/namespaces in C#?

I need to be able to create an XML Document that looks like this:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <rootprefix:rootname 
     noPrefix="attribute with no prefix"
     firstprefix:attrOne="first atrribute"
     secondprefix:attrTwo="second atrribute with different prefix">

     ...other elements...

 </rootprefix:rootname>

Here's my code:

XmlDocument doc = new XmlDocument();

XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
doc.AppendChild(declaration);

XmlElement root = doc.CreateElement("rootprefix:rootname", nameSpaceURL);
root.SetAttribute("schemaVersion", "1.0");

root.SetAttribute("firstprefix:attrOne", "first attribute");
root.SetAttribute("secondprefix:attrTwo", "second attribute with different prefix");

doc.AppendChild(root);

Unfortunately, what I'm getting for the second attribute with the second prefix is no prefix at all. It's just "attrTwo"--like the schemaVersion attribute.

So, is there a way to have different prefixes for attributes in the root element in C#?

like image 283
Narnian Avatar asked Jul 02 '13 14:07

Narnian


People also ask

How do I add namespace prefix to XML element?

When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

Can XML have multiple namespaces?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.

Can XML attributes have namespaces?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

How do I change the default namespace in XML?

You can change the default namespace within a particular element by adding an xmlns attribute to the element. Example 4-4 is an XML document that initially sets the default namespace to http://www.w3.org/1999/xhtml for all the XHTML elements.


1 Answers

This is just a guide for you. May be you can do:

        NameTable nt = new NameTable();
        nt.Add("key");

        XmlNamespaceManager ns = new XmlNamespaceManager(nt);
        ns.AddNamespace("firstprefix", "fp");
        ns.AddNamespace("secondprefix", "sp");

        root.SetAttribute("attrOne", ns.LookupPrefix("fp"), "first attribute");

        root.SetAttribute("attrTwo", ns.LookupPrefix("sp"), "second attribute with different prefix");

This will result in:

        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <rootprefix:rootname schemaVersion="1.0" d1p1:attrOne="first attribute" d1p2:attrTwo="second attribute with different prefix" xmlns:d1p2="secondprefix" xmlns:d1p1="firstprefix" xmlns:rootprefix="ns" />

Hope this will be of any help!

like image 187
Azhar Khorasany Avatar answered Sep 28 '22 02:09

Azhar Khorasany