views:

1048

answers:

2

Im having trouble generating XML along the lines of this:

<Root xmlns:brk="http://somewhere"&gt;
<child1>
     <brk:node1>123456</brk:node1>
     <brk:node2>500000000</brk:node2>
</child1>
</Root>

This code get me most of the way but I cant get the 'brk' namespace infront of the nodes;

 var rootNode = new XElement("Root");
 rootNode.Add(new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere"));

 var childNode = new XElement("child1");
 childNode.Add(new XElement("node1",123456));
 rootNode.Add(childNode);

Iv tried this:

XNamespace brk = "http://somewhere";
childNode.Add(new XElement(brk+"node1",123456));

and this

XNamespace brk = "http://somewhere";
childNode.Add(new XElement("brk:node1",123456));

but both cause exceptions.

+2  A: 

You are almost there, but you made one simple error in your first code example. I believe this is what you require:

XNamespace brk = "http://somewhere.com";
XElement root = new XElement("Root",
    new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere.com"));

XElement childNode = new XElement("child1");
childNode.Add(new XElement(brk + "node1",123456));
root.Add(childNode);

The main difference here is where I add node1 to childNode as follows:

childNode.Add(new XElement(brk + "node1",123456));

This code, given an XmlWriter and XDocument gives me the output:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:brk="http://somewhere.com"&gt;
  <child1>
    <brk:node1>123456</brk:node1>
  </child1>
</Root>

See MSDN for details of using XNamespace.

Jeff Yates
Ok Yates, lets have a man hug, your just on the back foot because your envious of my var usage. I cant see why yours should work and not mine? can you tell me?
Dan
I'm down with the hug, but we'll have to keep that dirty var usage out of it - I don't want to catch anything.I think that this is working because in your first code example, you don't do the 'brk + "node1"', you just do "node1".
Jeff Yates
Interestingly, I tried your second code example where you include the XNamespace declaration and the `brk + "node1"` and I didn't get an exception, so I'm not sure why that didn't work for you.
Jeff Yates
Im not sure that is it!? Iv tried brk+"node1". It seems to be depended on whether you set the namepace up on the constructor of the rootelement or the Add method.
Dan
Nope, I changed that in my code too and it still works.
Jeff Yates
A: 

Seems odd that its fixed by setting up the namespace in the contrustor:

XElement root = new XElement("Root",
    new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere.com"));

Adversed to using the add method:

var rootNode = new XElement("Root");
 rootNode.Add(new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere"));

Slighty related how would I define a namespace like this:

<Root xsi:brr="http://somewhere.com"&gt;

</Root>

attempts such as this seem to also fail:

XNamespace xsi = "http://somewhere";
new XAttribute(xsi+"brr", @"http://somewhere");

This produces duplicate tag exception, and this:

new XAttribute("xsi:brr", @"http://somewhere");

Complains about the ':'

Dan
For me, both the constructor setup and the Add approach work - what problem do you see?
Jeff Yates