Is it possible to query an XML file with SQL?

I am currently working on a case where we do not want to change much in the C # / wpf program, but would like to add a function. We currently allow certain users to add SQL queries to the database to retrieve client data, so you must specify a custom connection string / provider name. Using this information, you can create a connection and retrieve data using C #.

However, we would like to add the ability to allow this user group to also request XML files with a specific connection / provider string. I was just looking for opportunities in .net to do this, but can't find a decent way ... Is something like that possible? (Perhaps OLEDB / ODBC)?

edit: For clarity, I would like to state that the solution should be able to fit into the data source connection template with the specified connection string with the specified provider and execute an SQL query.

edit2: Having examined the first three answers, I decided to look beyond XML. This post seems to illustrate the above case in the best way (the only difference is that instead of XLS it is used instead of XML): How to request an excel file in C # with a detailed request . Possible XML solutions are still welcome, however ...

Thanks in advance.

+3
source share
4 answers

XML XPath XML-, SQL, . , XPath , SQL - XML . , , , , SQL- .

XPath , SQL, (XQuery), . XQuery XML. XQuery - SQL, .

( SQL Server) XML, Xpath SQL-. CROSS APPLY, , SQL. , , .

, , XML-, , .

, XML , . .

0

. Linq2Xml

http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

http://www.liquidcognition.com/tech-tidbits/linq2xml-example.aspx

// Loading from a file, you can also load from a stream
XDocument loaded = XDocument.Load(@"C:\contacts.xml");


// Query the data and write out a subset of contacts
var q = from c in loaded.Descendants("contact")
        where (int)c.Attribute("contactId") < 4
        select (string)c.Element("firstName") + " " +
      (string)c.Element("lastName");


foreach (string name in q)
    Console.WriteLine("Customer name = {0}", name);
+4

Linq - , SQL .NET, SQL . XML-.

Linq XML.

, /

var q = from c in xmlSource.contact
        where c.contactId < 4
        select c.firstName + " " + c.lastName;
-1

Source: https://habr.com/ru/post/1742780/


All Articles