Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding xmlns namespace in an Xdocument

I want to create an XDocument with whcih will look like below:

<configurations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://msn.com/csl/featureConfigurationv2">
  <configuration>
    …
  </configuration>
</configurations>

I am facing problem in adding the second attribute. I am trying this:

XYZ.Element("configurations").SetAttributeValue("xmlns", "http://msn.com/csl/featureConfigurationv2");

But its not adding the attribute.

Can you suggest something else please.

like image 902
Jash Avatar asked Nov 14 '22 12:11

Jash


1 Answers

Try this way

XNamespace ns = XNamespace.Get("http://msn.com/csl/featureConfigurationv2"); 
XDocument doc = new XDocument(
    // Do XDeclaration Stuff
    new XElement("configurations",
        new XAttribute(XNamespace.Xmlns, ns),
        // Do XElement Stuff
     )
);

and this way too

XNamespace ns = "http://msn.com/csl/featureConfigurationv2";
XElement configurations = new XElement(ns + "configurations",
    new XAttribute("xmlns", "http://msn.com/csl/featureConfigurationv2"),
    // Do XElement Stuff
);
like image 191
Siva Charan Avatar answered Nov 16 '22 02:11

Siva Charan