Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query an XML file containing nested elements using LINQPad?

I'm using LINQPad to query and visualize XML files with C#. For example:

var xml = XElement.Load(@"C:\file.xml");
xml.Elements().Where(e => e.Element("trHeader").Element("trTickNum").Value == "1").Dump();

However, I'd like run a query using SQL rather than C#.

Is there a way to load an XML which contains nested elements and query its table(s) using LINQPad's SQL option?

like image 910
Ryan Andres Avatar asked Aug 12 '11 23:08

Ryan Andres


2 Answers

This works for me.

var xml = XElement.Load(@"C:\AllTypesList.xml");
var list = xml.Elements().ToList();
var types = list.Where(x => x.Name == "XmlParamType").ToList();
types.Count().Dump();
types.GroupBy(t => t.Element("TypeName").Value).Count().Dump();
like image 72
thomas nn Avatar answered Oct 16 '22 06:10

thomas nn


That's not possible. The SQL option requires a database to be specified and is used to query that database. It's not possible to use SQL against an XML file which has its own hierarchy. What you could do is figure out a way to load the XML into SQL, or use the XML data type in SQL, then operate on the data entirely using SQL statements.

like image 20
Ahmad Mageed Avatar answered Oct 16 '22 05:10

Ahmad Mageed