User Tools

Site Tools


notes:csharp:linq_to_xml

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:csharp:linq_to_xml [2017/06/09]
leszek [Query XML]
notes:csharp:linq_to_xml [2017/06/09] (current)
leszek [Transform XML]
Line 464: Line 464:
   </​books>​   </​books>​
 </​books>​ </​books>​
 +</​code>​
 +
 +
 +====XML Namespaces====
 +
 +<code csharp>
 +// Create a namespace.
 +XNamespace ns = "​http://​something.com/​stuff/​2016";​
 +
 +// Create elements within a default namespace ns. Note that the + operator is overloaded.
 +var xml = new XElement(ns + "​books", ​
 +    from b in books // books is a collection of books
 +    select new XElement(ns + "​Book",​
 +        new XAttribute("​title",​ b.Title))
 +    );
 +
 +// Create elements within a different namespace (without a prefix).
 +XNamespace ext = "​http://​something.com/​stuff/​2016/​extended";​
 +var xml = new XElement(ns + "​books", ​
 +    from b in books 
 +    select new XElement(ext + "​Book",​
 +        new XAttribute("​title",​ b.Title))
 +    );
 +
 +// Create elements within a different namespace (with a prefix).
 +XNamespace ext = "​http://​something.com/​stuff/​2016/​extended";​
 +var xml = new XElement(ns + "​books", ​
 +    from b in books 
 +    select new XElement(ext + "​Book",​
 +        new XAttribute("​title",​ b.Title))
 +    );
 +xml.Add(new XAttribute(XNamespace.Xmlns + "​ext",​ ext));
 </​code>​ </​code>​
  
notes/csharp/linq_to_xml.txt ยท Last modified: 2017/06/09 by leszek