Monday 15 April 2013

A C# helper to read and write XML from and to objects

I really like jQuery?s pattern of attribute getters and setters. They are fluent and work really well with HTML and XML DOMs. If you specify a value in addition to the name, it?s setting, otherwise it?s getting. In C#, we have an OK API for XML, XElement, but it?s not as easy to use as jQuery?s attr methods. It is also missing the flexibility of Javascript with regards to parameter types. To recreate the simplicity of attr in C#, I built a set of extension methods for the most common simple types: var el = new XElement("node"); el.Attr("foo", "bar") .Attr("baz", 42) .Attr("really", true); var answer = el.Attr("baz"); The element built by this code looks like this: <node foo="bar"...(read more)
crawled from : Asp

No comments:

Post a Comment