The XML class library has been available since the birth of. NET, but it is very inconvenient to use. NET. For example, when we need to construct an XML document, we need to use the dom api as follows:
Var xmlDoc = new XmlDocument (); var rootEle = xmlDoc. createElement ("persons"); xmlDoc. appendChild (rootEle); var person1 = xmlDoc. createElement ("person"); person1.InnerText = "Tom"; var person1Age = xmlDoc. createAttribute ("age"); person1Age. value = "10"; person1.Attributes. append (person1Age); rootEle. appendChild (person1); var person2 = xmlDoc. createElement ("person"); person2.InnerText = "Jerry"; var person2Age = xmlDoc. createAttribute ("age"); person2Age. value = "8"; person2.Attributes. append (person2Age); rootEle. appendChild (person2 );
Don't look at so many lines of code, but in fact it only constructs such a simple XML:
<Persons> <person age = "10"> Tom </person> <person age = "8"> Jerry </person> </persons>
I admit that DOM APIs are indeed very rigorous (such as the attribution relationship between XmlDocument and XmlElement) and very well-defined and object-oriented, but this is easy to use. I remember that in or 04, I made a WinForm application for editing XML documents for the project. At that time, it was not as easy to think of the "lazy" approach as it is now, VS 2003 is not as easy as VS 2005/2008, so it is hard to work. This situation has not changed in. NET 2.0. Until one day, the emergence of LINQ to XML came along with. NET 3.5, so the XML life suddenly became much better. For example, the above functions can be implemented in just a few lines:
Var xmlDoc = new XElement ("persons", new XElement ("person", "Tom", new XAttribute ("age", 10), new XElement ("person ", "Jerry", new XAttribute ("age", 8 )));
Although LINQ to XML has always been part of the LINQ feature in C #3.0 ...... Some other parallel expressions, but I always think that LINQ to XML is a special form of LINQ to Object, but it is only used to operate XML. All of it is the function of the relevant class library (such as XElement) in the namespace of System. Xml. XElement and other related types greatly simplify our development. Compared with DOM APIs, XML construction and reading are much easier. But as the saying goes: "I am afraid of goods comparison if I don't know the goods." There is still a significant gap between such APIs and Ruby Markup Builder. See:
Builder = Builder: XmlMarkup. newxml = builder. persons {| B. person ("Tom",: age => "10") B. person ("Jerry",: age => "8 ")}
Please refer to the code above. it naturally does not use the standard coloring method in Ruby. The purpose of my coloring is body.