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

2

6

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

Lemal answered 2/7, 2013 at 14:48 Comment(0)
G
2

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!

Gunman answered 2/7, 2013 at 16:8 Comment(1)
worth noting that the NameTable and AddNameSpace is needed only if you need to determine the shorthand of the namespace rather then the default naming convention (d1p1, d1p2, ...)Zephan
L
1

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);
Lemal answered 3/7, 2013 at 12:50 Comment(1)
This is the fastest solution. Thanks for this. I did this too, and I replaced InnerXml later with the desired XML. I think it's extremely hard to customize the output just the way you want it with standard APIs. You have no control in what order attributes are written.Funicle

© 2022 - 2024 — McMap. All rights reserved.