29

This may be a beginner xml question, but how can I generate an xml document that looks like the following?

<root xmlns:ci="http://somewhere.com" xmlns:ca="http://somewhereelse.com">
    <ci:field1>test</ci:field1>
    <ca:field2>another test</ca:field2>
</root>

If I can get this to be written, I can get the rest of my problem to work.

Ideally, I'd like to use LINQ to XML (XElement, XNamespace, etc.) with c#, but if this can be accomplished easier/better with XmlDocuments and XmlElements, I'd go with that.

Thanks!!!

Chris Conway
  • 16,269
  • 23
  • 96
  • 113

5 Answers5

55

Here is a small example that creates the output you want:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        XNamespace ci = "http://somewhere.com";
        XNamespace ca = "http://somewhereelse.com";

        XElement element = new XElement("root",
            new XAttribute(XNamespace.Xmlns + "ci", ci),
            new XAttribute(XNamespace.Xmlns + "ca", ca),
                new XElement(ci + "field1", "test"),
                new XElement(ca + "field2", "another test"));
    }
}
techvice
  • 1,315
  • 1
  • 12
  • 24
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • Don't you need colons in there for that to work? Also, doesn't `XNamespace.Xmlns` output `http://www.w3.org/2000/xmlns/`? – BrainStorm.exe Apr 14 '16 at 18:06
  • 1
    @BrainStorm.exe No. As originally answered, the code works as expected. When the XNamespace is added with the string, the colon is automatically added in. This is not something that has to be manually performed. – techvice May 11 '16 at 19:00
  • 1
    Here is the [documentation detailing the addition operator for XNamespace and a string](https://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace.op_addition(v=vs.110).aspx) – techvice May 11 '16 at 19:15
1

Try this code:

string prefix = element.GetPrefixOfNamespace(element.Name.NamespaceName);
string name = String.Format(prefix == null ? "{1}" : "{0}:{1}", prefix, element.Name.LocalName);`
Michał
  • 2,456
  • 4
  • 26
  • 33
Hariharan
  • 39
  • 4
0

I hope you will get some useful information from this thread - XElement default namespace on attributes provides unexpected behaviour

EDIT:

Another FAQ at - http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/c0648840-e389-49be-a3d2-4d5db17b8ddd

Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
-1
XNamespace ci = "http://somewhere.com";
XNamespace ca = "http://somewhereelse.com";
XElement root = new XElement(aw + "root",
    new XAttribute(XNamespace.Xmlns + "ci", "http://somewhere.com"),
    new XAttribute(XNamespace.Xmlns + "ca", "http://somewhereelse.com"),
    new XElement(ci + "field1", "test"),
    new XElement(ca + "field2", "another test")
);
Console.WriteLine(root);

This should output

<root xmlns:ci="http://somewhere.com" xmlns:ca="http://somewhereelse.com">
    <ci:field1>test</ci:field1>
    <ca:field2>another test</ca:field2>
</root>
blparker
  • 343
  • 4
  • 12
-2

For XmlDocument it's similar:

XmlAttribute attribute1 = sessionXml.CreateAttribute("s", "Attribute1", namespaceURI);
XmlAttribute attribute2 = sessionXml.CreateAttribute("s", "Attribute2", namespaceURI);
XmlAttribute attribute3 = sessionXml.CreateAttribute("s", "Attribute3", namespaceURI);
XmlAttribute attribute4 = sessionXml.CreateAttribute("s", "Attribute4", namespaceURI);