Aloha ! So I’m back, this time to discuss how to query an xml document using LINQ, as I promised.
Let’s first see how to create an xml tree from scratch. In object oriented programming, an xml tree is created in a bottom – up manner.
Let’s first see how to create an xml tree from scratch. In object oriented programming, an xml tree is created in a bottom – up manner.
XmlDocument doc = new XmlDocument();
XmlElement name = doc.CreateElement("name"
XmlElement name = doc.CreateElement("name"
);
name.InnerText = "Praveen";
XmlElement phone1 = doc.CreateElement("phone");
phone1.SetAttribute("type", "home");
phone1.InnerText = "206-555-0144";
XmlElement phone2 = doc.CreateElement("phone");
phone2.SetAttribute("type", "work");
phone2.InnerText = "425-555-0145"; XmlElement street1 = doc.CreateElement("street1");
street1.InnerText = "123 MG Road";
XmlElement city = doc.CreateElement("city");
city.InnerText = "Mumbai";
XmlElement state = doc.CreateElement("state");
state.InnerText = "Maharashtra";
XmlElement postal = doc.CreateElement("postal");postal.InnerText = "68042";
XmlElement address = doc.CreateElement("address");
address.AppendChild(street1);
address.AppendChild(city);
address.AppendChild(state);
address.AppendChild(postal);
XmlElement contact = doc.CreateElement("contact");
contact.AppendChild(name);contact.AppendChild(phone1);
contact.AppendChild(phone2);
contact.AppendChild(address);
XmlElement contacts = doc.CreateElement("contacts");
contacts.AppendChild(contact);doc.AppendChild(contacts);
name.InnerText = "Praveen";
XmlElement phone1 = doc.CreateElement("phone");
phone1.SetAttribute("type", "home");
phone1.InnerText = "206-555-0144";
XmlElement phone2 = doc.CreateElement("phone");
phone2.SetAttribute("type", "work");
phone2.InnerText = "425-555-0145"; XmlElement street1 = doc.CreateElement("street1");
street1.InnerText = "123 MG Road";
XmlElement city = doc.CreateElement("city");
city.InnerText = "Mumbai";
XmlElement state = doc.CreateElement("state");
state.InnerText = "Maharashtra";
XmlElement postal = doc.CreateElement("postal");postal.InnerText = "68042";
XmlElement address = doc.CreateElement("address");
address.AppendChild(street1);
address.AppendChild(city);
address.AppendChild(state);
address.AppendChild(postal);
XmlElement contact = doc.CreateElement("contact");
contact.AppendChild(name);contact.AppendChild(phone1);
contact.AppendChild(phone2);
contact.AppendChild(address);
XmlElement contacts = doc.CreateElement("contacts");
contacts.AppendChild(contact);doc.AppendChild(contacts);
However this format gives us a little clue about the tree structure at a glance. The other way is to follow a functional construction approach like this:
XElement contacts =
new XElement("contacts",new XElement("contact",
new XElement("name", "Praveen"),
new XElement("phone", "206-555-0144",
new XAttribute("type", "home")),
new XElement("phone", "425-555-0145",
new XAttribute("type", "work")),
new XElement("address",new XElement("street1", "123 MG Road"),
new XElement("city", "Mumbai"),
new XElement("state", "Maharashtra"),
new XElement("postal", "68042")
)));
new XElement("contacts",new XElement("contact",
new XElement("name", "Praveen"),
new XElement("phone", "206-555-0144",
new XAttribute("type", "home")),
new XElement("phone", "425-555-0145",
new XAttribute("type", "work")),
new XElement("address",new XElement("street1", "123 MG Road"),
new XElement("city", "Mumbai"),
new XElement("state", "Maharashtra"),
new XElement("postal", "68042")
)));
Note that by following indentation, the tree structure becomes a lot clearer.
It is not necessary to have an xml document to work on. You can create a whole new document using the Xlinq framework along with a declaration, comments and processing instructions:
XDocument contactsDoc =new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XComment("XLinq Contacts XML Example"),
new XProcessingInstruction("MyApp", "123-44-4444"),
new XElement("contacts",
new XElement("contact",
new XElement("name", "Praveen"),
new XElement("phone", "206-555-0144"),
new XElement("address",
new XElement("street1", "123 MG Road"),
new XElement("city", "Mumbai"),
new XElement("state", "Maharashtra"),
new XElement("postal", "68042")))));
new XDeclaration("1.0", "UTF-8", "yes"),
new XComment("XLinq Contacts XML Example"),
new XProcessingInstruction("MyApp", "123-44-4444"),
new XElement("contacts",
new XElement("contact",
new XElement("name", "Praveen"),
new XElement("phone", "206-555-0144"),
new XElement("address",
new XElement("street1", "123 MG Road"),
new XElement("city", "Mumbai"),
new XElement("state", "Maharashtra"),
new XElement("postal", "68042")))));
The output would be something like:
Now let’s see how to query an xml tree using LINQ.
Say to query a contact record from the above mentioned xml. This can be done as
var x =
from c in contacts.Elements("contact")
select new object[]
{
new XComment("contact"),
new XElement("name", (string)c.Element("name")),
c.Elements("phone"),
new XElement("address", c.Element("address"))
};
XLinq architecture facilitates end to end xml handling including traversing, modifying and manipulating xml.
To get a deep dive into it visit :
No comments:
Post a Comment