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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML doc getting blank (empty) xmlns on every element 1

Status
Not open for further replies.

jefftoaster

Programmer
Feb 6, 2003
19
0
0
US
This simple chunk of code:
XmlDocument doc = new XmlDocument();
XmlNode node;
node = doc.CreateElement("requestMessage","urn:test:test2");
doc.AppendChild(node);
node = doc.CreateElement("merchantID");
node.InnerText = "midgoeshere";
doc.DocumentElement.AppendChild(node);
XmlTextWriter tw = new XmlTextWriter("tester.xml", Encoding.ASCII);
tw.Formatting = Formatting.Indented;
doc.Save(tw);

Gives me this:
<?xml version="1.0" encoding="us-ascii"?>
<requestMessage xmlns="urn:test:test2">
<merchantID xmlns="">midgoeshere</merchantID>
</requestMessage>

QUESTION: What's with the xmlns=""? It comes out on every sub-element, and I don't want that. NOTE: I have to create the document this way (otherwise I'd just use xmlwriter to do everything).
 
xmlns is the Xml Namespace. You are setting it in the 2nd parameter of the CreateElement() method. If you change your code to be like this it goes away.

Code:
XmlDocument doc = new XmlDocument();
            XmlNode node;
            node = doc.CreateElement("requestMessage");
            doc.AppendChild(node);
            node = doc.CreateElement("merchantID");
            node.InnerText = "midgoeshere";
            doc.DocumentElement.AppendChild(node);
            XmlTextWriter tw = new XmlTextWriter("tester.xml", Encoding.ASCII);
            tw.Formatting = Formatting.Indented;
            doc.Save(tw);

And when I run that, I get this:

<?xml version="1.0" encoding="us-ascii"?>
<requestMessage>
<merchantID>midgoeshere</merchantID>
</requestMessage>

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
I do need the namespace of the <requestMessage> element, just not the xmlns="" of the <merchantID xmlns=""> element (and every element after that when I include additional elements).

I need:
<?xml version="1.0" encoding="us-ascii"?>
<requestMessage xmlns="urn:test:test2">
<merchantID>midgoeshere</merchantID>
<otherelement>whatever</otherelement>
</requestMessage>
 
Does this work? (using Guru7777's example)
Code:
XmlDocument doc = new XmlDocument();
            XmlNode node;
            node = doc.CreateElement("requestMessage");[red] 
            [b]node.SetAttribute("xmlns","urn:test:test2");[/b][/red] 
            doc.AppendChild(node);
            node = doc.CreateElement("merchantID");
            node.InnerText = "midgoeshere";
            doc.DocumentElement.AppendChild(node);
            XmlTextWriter tw = new XmlTextWriter("tester.xml", Encoding.ASCII);
            tw.Formatting = Formatting.Indented;
            doc.Save(tw);
 
You've to let it known that of the child node is intended to inherit the default namespace of its parent otherwise it is taken that the child being without a namespace.
[tt]
node = doc.CreateElement("requestMessage","urn:test:test2");
doc.AppendChild(node);
node = doc.CreateElement("merchantID"[red],"urn:test:test2"[/red]);
node.InnerText = "midgoeshere";
doc.DocumentElement.AppendChild(node);
[/tt]
 
tsuji - your solution was what worked for me.
ALL - Thanks for your time and help!!

-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top