The XML part
Now that we have an understanding of LINQ, it's time to move on to the XML part.For this article, we will be using this XML file:
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee id="1" salaried="no">
<name>Gustavo Achong</name>
<hire_date>7/31/1996</hire_date>
</employee>
<employee id="3" salaried="yes">
<name>Kim Abercrombie</name>
<hire_date>12/12/1997</hire_date>
</employee>
<employee id="8" salaried="no">
<name>Carla Adams</name>
<hire_date>2/6/1998</hire_date>
</employee>
<employee id="9" salaried="yes">
<name>Jay Adams</name>
<hire_date>2/6/1998</hire_date>
</employee>
</employees>
The old way
In the previous versions of the .NET Framework, XML was document-centric; in other words, to create any structure, you first had to start with anXMLDocument.public class OldWay
{
private static XmlDocument m_doc = new XmlDocument();
public static void CreateEmployees()
{
XmlElement root = m_doc.CreateElement("employees");
root.AppendChild(AddEmployee(1, "Gustavo Achong",
DateTime.Parse("7/31/1996"), false));
root.AppendChild(AddEmployee(3, "Kim Abercrombie",
DateTime.Parse("12/12/1997"), true));
root.AppendChild(AddEmployee(8, "Carla Adams",
DateTime.Parse("2/6/1998"), false));
root.AppendChild(AddEmployee(9, "Jay Adams",
DateTime.Parse("2/6/1998"), false));
m_doc.AppendChild(root);
Console.WriteLine(m_doc.OuterXml);
}
private static XmlElement AddEmployee(int ID, string name,
DateTime hireDate, bool isSalaried)
{
XmlElement employee = m_doc.CreateElement("employee");
XmlElement nameElement = m_doc.CreateElement("name");
nameElement.InnerText = name;
XmlElement hireDateElement = m_doc.CreateElement("hire_date");
hireDateElement.InnerText = hireDate.ToShortDateString();
employee.SetAttribute("id", ID.ToString());
employee.SetAttribute("salaried", isSalaried.ToString());
employee.AppendChild(nameElement);
employee.AppendChild(hireDateElement);
return employee;
}
}
Smart developers would create helper methods to ease the pain, but it was still a verbose, cumbersome process. An XMLElement can't be created on its own, it must be created from an XMLDocument.
XmlElement employee = m_doc.CreateElement("employee");
Trying to do this generates a compiler error:
XmlElement employee = new XmlElement();
Looking at the above example, it is also difficult to get an idea about the scheme for this document.
The new way
Using the classes from theSystem.Xml.Linq namespace and the features available in .NET 3.5, constructing an XML document is very easy and very readable.public static void CreateEmployees()
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("A sample xml file"),
new XElement("employees",
new XElement("employee",
new XAttribute("id", 1),
new XAttribute("salaried", "false"),
new XElement("name", "Gustavo Achong"),
new XElement("hire_date", "7/31/1996")),
new XElement("employee",
new XAttribute("id", 3),
new XAttribute("salaried", "true"),
new XElement("name", "Kim Abercrombie"),
new XElement("hire_date", "12/12/1997")),
new XElement("employee",
new XAttribute("id", 8),
new XAttribute("salaried", "false"),
new XElement("name", "Carla Adams"),
new XElement("hire_date", "2/6/1998")),
new XElement("employee",
new XAttribute("id", 9),
new XAttribute("salaried", "false"),
new XElement("name", "Jay Adams"),
new XElement("hire_date", "2/6/1998"))
)
);
}
public XElement(XName name, Object[] content)
As we can see from one of the constructors for XElement, it takes an array of objects. In the example above, the employees element is constructed from four XElements, one for each employee, which in turn is constructed from XAttributes and other XElements.In the above example, we could have replaced
XDocument with XElement if we removed the XDeclaration and XComment objects. This is because the constructor XDocument used takes a XDeclaration instance, rather than the XName that the XElement constructor takes.public XDocument(XDeclaration declaration,Object[] content)
Another thing to note when running the demo is how both documents are printed to the console window.As we can see, the old method just streams the contents of the document to the console. The method does that also; however, it is nicely formatted with no extra effort.
For Reference Visit.....
No comments:
Post a Comment