Quantcast
Channel: Dynamic Toolkit - Query XML documents with DynamicXPath and more
Viewing all articles
Browse latest Browse all 22

Updated Wiki: Home

$
0
0
Project Description
Let's make querying data enjoyable for the masses. This library allows you to easily query XML documents using C# instead of embedded XPath strings. The library also contains a very lightweight Object Relational Mapper (ORM) using expression trees to strongly type the querying process. I'll tidy the ORM portion at a later time and upload it here. I've enjoyed using this ORM in the past for projects where the Entity Framework seemed like overkill.

For now, I'll simply introduce you to DynamicXPath...

The Old Way
XmlDocument document = new XmlDocument();
document.Load("contacts.xml");

XmlNodeList contactNameNodes = document.SelectNodes("/Phonebook/Contacts/Contact/Name"); // Hmm, a language within a language
IList<string> contactNames = new List<string>();

foreach (XmlNode contactNameNode in contactNameNodes)
{
    contactNames.Add(contactNameNode.InnerText);    
}


The DynamicXpath Way!
dynamic addressBook = DynamicXpath.Load("contacts.xml");
List<string> contactNames = addressBook.Contacts.Contact.Name;
Or even more concise if that's your cup of tea...
List<string> contactNames = DynamicXpath.Load("contacts.xml").Contacts.Contact.Name;

And some more things that you can do
These are just a few more things you can do with DynamicXpath. DynamicXPath also provides user-friendly ways to retrieve non-string node values (i.e. int, decimal, double, bool).
dynamic addressBook = DynamicXpath.LoadXml("contacts.xml");
                        
// Resolves to: "/*/Contacts/Contact[name = 'Tony']/Phone[1]/text()"string tonyPhoneNumber = addressBook.Contacts.Contact["Name = 'Tony'"].Phone;

// Resolves to: "/*/Contacts/Contact[name = 'Steph']/Phone[1]/text()"string stephPhoneNumber = addressBook.Contacts.Contact[() => p.Name == 'Steph'].Phone;  // Not supported by C# 4.0// Resolves to: "/*/Contacts/Contact[name = 'Tony' or name = 'Steph']/Phone[1]/text()"
List<string> tonyAndStephsPhoneNumbers = addressBook.Contacts.Contact[p => p.Name == "Tony" || p.Name == 'Steph'].Phone;  // Not supported by C# 4.0// Resolves to: "/*/Contacts/Contact/Phone"
XmlNodeList allPhoneNumberNodes = addressBook.Contacts.Contact.Phone;

// Resolves to: "/*/Contacts/Contact/Phone/text()"
List<string> allPhoneNumbers = addressBook.Contacts.Contact.Phone;

// Resolves to: "/*/Contacts/Contact[1]"
XmlNode firstContact = addressBook.Contacts.Contact;

// Resolves to: "/*/Contacts/Contact[Name = 'Tony']/Phone[1]/@Tag"string phoneTag = addressBook.Contacts.Contact["Name = 'Tony'"].Phone.Attribute("Tag");

// Enumerating over the dynamic XPathforeach (dynamic contact in addressBook.Contacts.Contact)
{
    Console.WriteLine("{0}, {1}", contact.Name, contact.Phone);
}

Enjoy
If you have any comments/suggestions, drop me a note or contribute to the project.

Viewing all articles
Browse latest Browse all 22

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>