5

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.

1 Answer 1

1

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
);
1
  • Why this answer has been accepted? Both snippets are totally invalid. The first one causes compile-time error, and the second one crashes in runtime. Jun 15, 2016 at 15:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.