Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Namespaces on the wrong node

Status
Not open for further replies.

srudin

Programmer
Jan 1, 2002
32
CH
i'm creating an XmlDocument using c#. the code looks about like this:

myXmlElement = XmlDocument.CreateElement("prefix", "name", "uri");
ParentElement.AppendChild(myXmlElement);

the xml structure is supposed to look about like this:

<root xmlns:ns1="uri1" xmlns:ns2="uri2" xmlns:ns3="uri3">
<ns1:a/>
<ns2:b/>
<ns3:c/>
</root>

unfortunately what i get is something like this:

<root>
<ns1:a xmlns:ns1="uri1"/>
<ns2:b xmlns:ns2="uri2"/>
<ns3:c xmlns:ns3="uri3"/>
</root>

how can i advise the XmlDocument that the namespace is supposed to be declared on a parent node? this way the declaration gets repeated for every node what blows up the document size for no reason.

thx in advance, best regards
 
[1] In vbs, it is done like this, modulo some whitespace formatting which is not essential.
[tt]
set oparser=createobject("msxml2.domdocument")
with oparser
.async=true
.validateonparse=false
end with
set oroot=oparser.createElement("root")
set oparser.documentelement=oroot
oroot.setAttribute "xmlns:ns1","uri1"
oroot.setAttribute "xmlns:ns2","uri2"
oroot.setAttribute "xmlns:ns3","uri3"

oroot.appendchild oparser.createtextnode(vbcrlf)
set onode=oparser.createelement("ns1:a")
oroot.appendchild onode

oroot.appendchild oparser.createtextnode(vbcrlf)
set onode=oparser.createelement("ns2:b")
oroot.appendchild onode

oroot.appendchild oparser.createtextnode(vbcrlf)
set onode=oparser.createelement("ns3:c")
oroot.appendchild onode

oroot.appendchild oparser.createtextnode(vbcrlf)

wscript.echo oparser.documentelement.xml
[/tt]
[2] In c#, you get the idea which is to setup the namespace declaration at the root level. It is slightly more involved. You need to create xmlns as prefix properly. Something like this.
[tt]
XmlDocument oparser=new XmlDocument();
XmlElement oroot=(XmlElement)oparser.AppendChild(xmlDoc.CreateElement("root"));

XmlAttribute oattr1=oparser.CreateAttribute("xmlns", "ns1", [ignore]"[/ignore]);
oattr1.Value = "uri1";
XmlAttribute oattr2=oparser.CreateAttribute("xmlns", "ns2", [ignore]"[/ignore]);
oattr2.Value = "uri2";
XmlAttribute oattr3=oparser.CreateAttribute("xmlns", "ns3", [ignore]"[/ignore]);
oattr3.Value = "uri3";

oroot.SetAttributeNode(oattr1);
oroot.SetAttributeNode(oattr2);
oroot.SetAttributeNode(oattr3);

oroot.AppendChild(oparser.CreateElement("ns1", "a", "uri1"));
oroot.AppendChild(oparser.CreateElement("ns2", "b", "uri2"));
oroot.AppendChild(oparser.CreateElement("ns3", "c", "uri3"));

xmlDoc.Save(Console.Out);
[/tt]
 
GREAT - that did the job, thx!

it can be coded a bit tighter though - for those interested:

if (myXmlDocument.DocumentElement.Attributes["xmlns:ns1"] == null)
{
myXmlDocument.DocumentElement.SetAttribute("xmlns:ns1", "uri1");
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top