简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM