Purna
Purna

Reputation: 57

How to add xmlns attribute to the root element?

I have to write xml file like the fallowing

<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <Status>Enabled</Status>
</VersioningConfiguration>

please any one help me to write like above.

Upvotes: 0

Views: 1954

Answers (3)

MovGP0
MovGP0

Reputation: 7755

Create XDocument

Here is how to create an XDocument properly:

XNamespace aws = "http://s3.amazonaws.com/doc/2006-03-01/";

XDocument doc = new XDocument(
    // add the XML declaration (optional)
    new XDeclaration("1.0", "utf-8", null),

    // add the root element
    // prefer to place namespace imports here
    new XElement(aws + "VersioningConfiguration",

       // creates an acronym for the namespace
       new XAttribute(XNamespace.Xmlns + "aws", aws),

       // IMPORTANT: remember to add the namespace to all elements and attributes
       new XElement(aws + "Status", "Enabled")
    )
);

// consider SaveOptions as required
string xml = doc.ToString(SaveOptions.OmitDuplicateNamespaces | SaveOptions.DisableFormatting);

Console.WriteLine(xml);

The result (with formatting) will be:

<aws:VersioningConfiguration xmlns:aws="http://s3.amazonaws.com/doc/2006-03-01/">
  <aws:Status>Enabled</aws:Status>
</aws:VersioningConfiguration>

Note on Culture-Dependent Formatting

Note that XElement, XAttribute will call the .ToString(CultureInfo.CurrentCulture) method on child elements. This might be an issue when different formatting rules are applied to double, float. bool, etc..

My recommendation is to always call the .ToString(CultureInfo.InvariantCulture) and maybe the .ToLower() methods explicitly to prevent formatting issues. Also remember to use the appropriate culture when parsing the XML file.

Note on using XmlWriter

If you want to write the XML to a file, consider the following:

var settings = new XmlWriterSettings
{
    Encoding = Encoding.UTF8,
    Indent = true
};

using (var writer = XmlWriter.Create("path/to/file.xml", settings))
{
    doc.Save(writer);
}

Note

XmlWriter.Create also acceps a Stream object, ie. if you want to write to

  • TextStream
  • FileStream
  • InMemoryStream
  • StringBuilder
  • etc.

Upvotes: 0

James
James

Reputation: 170

 XNamespace Name = "http://s3.amazonaws.com/doc/2006-03-01/";
 XDocument doc=new XDocument();
 XElement X1=new XElement(Name+"VersioningConfiguration","" );
 XElement X2=new XElement(Name+"Status","Enabled");
 X1.Add(X2);
 doc.Add(X1);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499790

LINQ to XML makes this trivial - you just specify the namespace for the element, and it will include the xmlns="..." automatically. You can give it an alias, but that's slightly harder. To produce the exact document you've shown, you just need:

XNamespace ns = "http://s3.amazonaws.com/doc/2006-03-01/";
var doc = new XDocument(
    new XElement(ns + "VersioningConfiguration",
       new XElement(ns + "Status", "Enabled")));
Console.WriteLine(doc);

LINQ to XML is by far the best XML API I've used, particularly in its handling of namespaces. Just say no to XmlDocument :)

Upvotes: 1

Related Questions