andyw27
asked on
Help search XML Files
Hello,
In my program I call a function which executes a query (with parameter) on the database. This can be called thousands of times.
This is not very efficient. I though a better approach might to be download all the data once to XML file, save that on the server and then query that each time.
To be able to try this I’m looking for suggestions as to how I can query an XML file and return an XMLNodeList containing the results?
I'd need to return records/record
<records xmlns:dt="urn:schemas-micr osoft-com: datatypes" >
<record>
<prefix>1234</prefix>
<title>Document 1</title>
<document_id>946</document _id>
<revision>1.1</revision>
<class_id>24</class_id>
<name>Type 01</name>
<description />
</record>
</records>
TIA
In my program I call a function which executes a query (with parameter) on the database. This can be called thousands of times.
This is not very efficient. I though a better approach might to be download all the data once to XML file, save that on the server and then query that each time.
To be able to try this I’m looking for suggestions as to how I can query an XML file and return an XMLNodeList containing the results?
I'd need to return records/record
<records xmlns:dt="urn:schemas-micr
<record>
<prefix>1234</prefix>
<title>Document 1</title>
<document_id>946</document
<revision>1.1</revision>
<class_id>24</class_id>
<name>Type 01</name>
<description />
</record>
</records>
TIA
MyDataSet.MyDataRow[] drArray = (MyDataSet.MyDataRow[])dat aSet.MyTab le.Select( "class_id=24", "SEQ asc" );
As suggested by #junges, it would be better to load your XML into a DataSet and query the DataSet.
You may also take the advantage of using a sql-like syntax (linq to dataset) by adding System.Data.DataSetExtensi ons dll to your references.
Please note that if your goal is performance, you may create a Class ("Entity") that reflects your query and serialize it to disk. You may then access it whenever you want (maybe until you want it to "expire", based on the date of the file).
I personally use this approach and find it very useful (but it needs a bit more initial developing).
Regards.
You may also take the advantage of using a sql-like syntax (linq to dataset) by adding System.Data.DataSetExtensi
Please note that if your goal is performance, you may create a Class ("Entity") that reflects your query and serialize it to disk. You may then access it whenever you want (maybe until you want it to "expire", based on the date of the file).
I personally use this approach and find it very useful (but it needs a bit more initial developing).
Regards.
@jonnidip
it would be better to load your XML into a DataSet and query the DataSet.Why is that?
it would be betterstands for "I prefer...". Sorry.
ASKER
Hi - thanks for the ideas. Do you have some examples of how you go about creating a Class ("Entity") that reflects your query and serialize it to disk ?
Loading to the dataset is an expensive operation, unless you require read/write operations of the record as a whole. (From your question you are using this as a cache to avoid expensive sql calls)
Your question only asks for "query an XML file and return an XMLNodeList containing the results?"
My previous post address that question, more complete code using the sample from link should be:
Your question only asks for "query an XML file and return an XMLNodeList containing the results?"
My previous post address that question, more complete code using the sample from link should be:
XmlDocument doc = new XmlDocument();
doc.Load("YourFilePathHere.xml");
XmlNodeList nodeList=root.SelectNodes(("//record");
where "//record" is an XPath expression that allows to limit the nodeList object to the record objects.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
where doc is your XmlDocument instance. For further details check link above.