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

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:
 
 

     ...other elements...

 

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#?

Solutions/Answers:

Answer 1:

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!

Answer 2:

I saw a post on another question that ended up solving the issue. I basically just created a string that had all the xml in it, then used the LoadXml method on an instance of XmlDocument.

string rootNodeXmlString = "<?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\" />";
doc.LoadXml(rootNodeXmlString);

References

cross-browser ‘getElementsByTagName’ with namespace from responseXML

cross-browser ‘getElementsByTagName’ with namespace from responseXML

Am trying to read an XML response using getElementsByTagName:
var rows = items.responseXML.getElementsByTagName("z:row");

for (var i=0; i

Solutions/Answers:

Answer 1:

This is what I use:

function byTagNS(xml,tag,ns) {
    return xml.getElementsByTagNameNS
      ? xml.getElementsByTagNameNS(ns,tag)
      : xml.getElementsByTagName(ns+":"+tag);
}

With in your case:

byTagNS(responseXML, "row", "z")

Answer 2:

If you don't care about the namespace then you could use the following:

xml.getElementsByTagNameNS("*", "yourElementHere")

This will fetch any element with the desired name regardless of which namespace it has or whether it has any namespace at all. Additionally, this should work as expected across different browsers.

See link for documentation.

References