3

I'm having trouble generating XML along the lines of this:

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

This code get me most of the way, but I can't get the 'brk' namespace in front 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);

I've 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dan
  • 29,100
  • 43
  • 148
  • 207
  • What exception do you get? I get no exception and correct results when using childNode.Add(new XElement(brk+"node1",123456)); – Vojislav Stojkovic Feb 09 '09 at 17:29
  • System.Xml.XmlException: The prefix '' cannot be redefined from '' to 'http://somewhere' within the same start element tag. – Dan Feb 09 '09 at 17:42

3 Answers3

3

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">
  <child1>
    <brk:node1>123456</brk:node1>
  </child1>
</Root>

See MSDN for details of using XNamespace.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • 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 Feb 09 '09 at 17:51
  • 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 Feb 09 '09 at 17:54
  • 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 Feb 09 '09 at 18:00
  • 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 Feb 09 '09 at 18:05
0

This is solotuion and working fine.

 using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.XPath;
    using System.Xml.Serialization;

    namespace CreateSampleXML
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            
                XNamespace xm = "http://somewhere.com";
                XElement rt= new XElement("Root", new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere.com"));
                XElement cNode = new XElement("child1");
                cNode.Add(new XElement(xm + "node1", 123456));
                cNode.Add(new XElement(xm + "node2", 500000000));
                rt.Add(cNode);
                XDocument doc2 = new XDocument(rt);
                doc2.Save(@"C:\sample3.xml");
            }
        }       
    }
0

I believe the problem is that the root element needs to have the namespace as well:

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

needs to be:

XElement root = new XElement(brk + "Root",
    new XAttribute(XNamespace.Xmlns + "brk", "http://somewhere.com"));
wm.wragg
  • 143
  • 1
  • 7