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.

2
  • What exception do you get? I get no exception and correct results when using childNode.Add(new XElement(brk+"node1",123456)); Commented Feb 9, 2009 at 17:29
  • System.Xml.XmlException: The prefix '' cannot be redefined from '' to 'somewhere' within the same start element tag. Commented Feb 9, 2009 at 17:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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".
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.
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.
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"));

Comments

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");
            }
        }       
    }

2 Comments

Please discuss your code on your answer. Don't just give the code.
That is solution. @EvertonAgner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.