You are almost there, but you made one simple mistake in your first code example. I believe this is what you need:
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 that I add node1
in childNode
as follows:
childNode.Add(new XElement(brk + "node1",123456));
This code given XmlWriter
and XDocument
gives me the result:
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:brk="http://somewhere.com">
<child1>
<brk:node1>123456</brk:node1>
</child1>
</Root>
For more information, XNamespace
see MSDN ..