75

How to create xml document with node prefix like:

<sphinx:docset>
  <sphinx:schema>
    <sphinx:field name="subject"/>
    <sphinx:field name="content"/>
    <sphinx:attr name="published" type="timestamp"/>
 </sphinx:schema>

When I try to run something like new XElement("sphinx:docset") i getting exception

Unhandled Exception: System.Xml.XmlException: The ':' character, hexadecimal val ue 0x3A, cannot be included in a name.
at System.Xml.XmlConvert.VerifyNCName(String name, ExceptionType exceptionTyp e)
at System.Xml.Linq.XName..ctor(XNamespace ns, String localName)
at System.Xml.Linq.XNamespace.GetName(String localName)
at System.Xml.Linq.XName.Get(String expandedName)

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Edward83
  • 6,664
  • 14
  • 74
  • 102

2 Answers2

124

It's really easy in LINQ to XML:

XNamespace ns = "sphinx";
XElement element = new XElement(ns + "docset");

Or to make the "alias" work properly to make it look like your examples, something like this:

XNamespace ns = "http://url/for/sphinx";
XElement element = new XElement("container",
    new XAttribute(XNamespace.Xmlns + "sphinx", ns),
    new XElement(ns + "docset",
        new XElement(ns + "schema"),
            new XElement(ns + "field", new XAttribute("name", "subject")),
            new XElement(ns + "field", new XAttribute("name", "content")),
            new XElement(ns + "attr", 
                         new XAttribute("name", "published"),
                         new XAttribute("type", "timestamp"))));

That produces:

<container xmlns:sphinx="http://url/for/sphinx">
  <sphinx:docset>
    <sphinx:schema />
    <sphinx:field name="subject" />
    <sphinx:field name="content" />
    <sphinx:attr name="published" type="timestamp" />
  </sphinx:docset>
</container>
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    thank you, but for first version I got it is not what I want;))) – Edward83 Feb 13 '11 at 18:37
  • @Edward83: See my other example. Basically you'll need the namespace to be specified in xmlns somewhere... – Jon Skeet Feb 13 '11 at 18:38
  • After all the ugly hacks I've used to do this (recursive static methods to attach namespace to everything) ... I tried this approach first, but I didn't prefix XNamespace.Xmlns to the outer namespace. Why is that prefix even necessary? Does it set it for global? – micahhoover Jun 21 '13 at 20:07
  • @micahhoover: You should read the W3C namespacing specification. It's not entirely clear to me what you were trying to achieve, or what went wrong. – Jon Skeet Jun 21 '13 at 21:38
22

You can read the namespace of your document and use it in queries like this:

XDocument xml = XDocument.Load(address);
XNamespace ns = xml.Root.Name.Namespace;
foreach (XElement el in xml.Descendants(ns + "whateverYourElementNameIs"))
    //do stuff
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • 2
    Nice approach (+1)! However, this didn't work for me in a case where the root XML element had multiple xmlns attributes, i.e.: xmlns:soapenv="..." xmlns="...". Using xml.Root.GetDefaultNamespace() did get me what I wanted, which was the value of the plain xmlns="..." attribute. – Jon Schneider May 20 '15 at 21:19