Just sharing some of my inconsequential lunch conversations with you... RSS  

Tuesday, February 26, 2008

Cool ways to write XML

Here are too cool ways to write XML. The first one depends on .NET 3.5:

XDocument xd = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("root",
new XElement("user",
new XElement("username", "orca"),

new XElement("realname", "Mark S. Rasmussen"),

new XElement("description", "I'll handle any escaping (like < & > for example) needs automagically."),

new XElement("articles",

new XElement("article", new XAttribute("id", "25"), "Handling DBNulls"),

new XElement("article", new XAttribute("id", "26"), "Accessing my privates")),

new XElement("hobbies",

new XElement("hobby", "Fishing"),

new XElement("hobby", "Photography"),

new XElement("hobby", "Work")

)

)

)

);

And here is a cool ways using the fluent interface pattern:

XmlOutput xo = new XmlOutput()
.XmlDeclaration()
.Node("root").Within()
.Node("user").Within()
.Node("username").InnerText("orca")
.Node("realname").InnerText("Mark S. Rasmussen")
.Node("description").InnerText("I'll handle any escaping (like < & > for example) needs automagically.")
.Node("articles").Within()
.Node("article").Attribute("id", "25").InnerText("Handling DBNulls")
.Node("article").Attribute("id", "26").InnerText("Accessing my privates")
.EndWithin()
.Node("hobbies").Within()
.Node("hobby").InnerText("Fishing")
.Node("hobby").InnerText("Photography")
.Node("hobby").InnerText("Work");

This last doesn't depend on .NET 3.5, just a this cool library from Mark S. Rasmussen.

No comments:

Development Catharsis :: Copyright 2006 Mário Romano