472,989 Members | 3,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

How to query on a xml document like SQL?

Dear Members,
I want to find a collection of nodes with certain attributes.
For example I want to retrieve every books with name = “NLP of MT”
from this XML document:

<inventory>

<books>
<book Name = “CS”</book>
<book Name = “NLP of MT”</book>
<book Name = “MT”</book>
</books>

</inventory>

This can be easily expressed in SQL like:
“ SELECT * FROM INVENTROY.BOOKS WHERE NAME = ‘NLP of MT’ “

How can I do this with XPath? Other ways are highly welcomed.
Please provide me a clear snippet of code.

Many thanks in advance,
Jun 27 '08 #1
6 1474
AliRezaGoogle wrote:
I want to find a collection of nodes with certain attributes.
For example I want to retrieve every books with name = “NLP of MT”
from this XML document:

<inventory>

<books>
<book Name = “CS”</book>
<book Name = “NLP of MT”</book>
<book Name = “MT”</book>
</books>

</inventory>

This can be easily expressed in SQL like:
“ SELECT * FROM INVENTROY.BOOKS WHERE NAME = ‘NLP of MT’ “

How can I do this with XPath? Other ways are highly welcomed.
The XPath expression "//inventory/books/book[@Name='NLP of MT']" should
do it.

Alternatively LINQ for XML could be an option.

Arne
Jun 27 '08 #2
AliRezaGoogle <as*******@yahoo.comwrote:
Dear Members,
I want to find a collection of nodes with certain attributes.
For example I want to retrieve every books with name = =3FNLP of MT=3F
from this XML document:

<inventory>

<books>
<book Name = =3FCS=3F</book>
<book Name = =3FNLP of MT=3F</book>
<book Name = =3FMT=3F</book>
</books>

</inventory>

This can be easily expressed in SQL like:
=3F SELECT * FROM INVENTROY.BOOKS WHERE NAME = =3FNLP of MT=3F =3F

How can I do this with XPath? Other ways are highly welcomed.
Please provide me a clear snippet of code.
Are you able to use .NET 3.5 and LINQ to XML? If so, that would provide
the most readable query syntax (IMO). Otherwise, XPath is the way to
go. If you could give us more details of this, we can code up an
example.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #3
On May 25, 5:41 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
AliRezaGoogle <asemoo...@yahoo.comwrote:
Dear Members,
I want to find a collection of nodes with certain attributes.
For example I want to retrieve every books with name = =3FNLP of MT=3F
from this XML document:
<inventory>
<books>
<book Name = =3FCS=3F</book>
<book Name = =3FNLP of MT=3F</book>
<book Name = =3FMT=3F</book>
</books>
</inventory>
This can be easily expressed in SQL like:
=3F SELECT * FROM INVENTROY.BOOKS WHERE NAME = =3FNLP of MT=3F =3F
How can I do this with XPath? Other ways are highly welcomed.
Please provide me a clear snippet of code.

Are you able to use .NET 3.5 and LINQ to XML? If so, that would provide
the most readable query syntax (IMO). Otherwise, XPath is the way to
go. If you could give us more details of this, we can code up an
example.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
No. I can not use what you mentioned. I use .Net Framework 2 with
VS2005
Jun 27 '08 #4
Well then you'll need to use XPath as Arne suggested

"AliRezaGoogle" <as*******@yahoo.comwrote in message
news:7f**********************************@k30g2000 hse.googlegroups.com...
On May 25, 5:41 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
>AliRezaGoogle <asemoo...@yahoo.comwrote:
Dear Members,
I want to find a collection of nodes with certain attributes.
For example I want to retrieve every books with name = =3FNLP of MT=3F
from this XML document:
<inventory>
<books>
<book Name = =3FCS=3F</book>
<book Name = =3FNLP of MT=3F</book>
<book Name = =3FMT=3F</book>
</books>
</inventory>
This can be easily expressed in SQL like:
=3F SELECT * FROM INVENTROY.BOOKS WHERE NAME = =3FNLP of MT=3F
=3F
How can I do this with XPath? Other ways are highly welcomed.
Please provide me a clear snippet of code.

Are you able to use .NET 3.5 and LINQ to XML? If so, that would provide
the most readable query syntax (IMO). Otherwise, XPath is the way to
go. If you could give us more details of this, we can code up an
example.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

No. I can not use what you mentioned. I use .Net Framework 2 with
VS2005
Jun 27 '08 #5
AliRezaGoogle wrote:
On May 25, 5:41 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
>AliRezaGoogle <asemoo...@yahoo.comwrote:
>>Dear Members,
I want to find a collection of nodes with certain attributes.
For example I want to retrieve every books with name = =3FNLP of MT=3F
from this XML document:
<inventory>
<books>
<book Name = =3FCS=3F</book>
<book Name = =3FNLP of MT=3F</book>
<book Name = =3FMT=3F</book>
</books>
</inventory>
This can be easily expressed in SQL like:
=3F SELECT * FROM INVENTROY.BOOKS WHERE NAME = =3FNLP of MT=3F =3F
How can I do this with XPath? Other ways are highly welcomed.
Please provide me a clear snippet of code.
Are you able to use .NET 3.5 and LINQ to XML? If so, that would provide
the most readable query syntax (IMO). Otherwise, XPath is the way to
go. If you could give us more details of this, we can code up an
example.

No. I can not use what you mentioned. I use .Net Framework 2 with
VS2005
Then use XPath.

Arne
Jun 27 '08 #6
AliRezaGoogle <as*******@yahoo.comwrote:

<snip>
No. I can not use what you mentioned. I use .Net Framework 2 with
VS2005
Okay, so you want XPath, probably. Here's a short but complete program
to demonstrate:

using System;
using System.Xml;

public class Test
{
const string SampleXml = @"
<inventory>
<books>
<book Name = 'CS'>First content</book>
<book Name = 'NLP of MT'>Second content</book>
<book Name = 'MT'>Third content</book>
</books>
</inventory>";

static void Main()
{
XmlDocument doc = new XmlDocument();
// You'd normally load from a file here, or
// something like that
doc.LoadXml(SampleXml);
XmlNode node = doc.SelectSingleNode
("/inventory/books/book[@Name='NLP of MT']");

Console.WriteLine("Found: {0}", node.InnerText);
}
}

Note that if you receive the name dynamically and it could be arbitrary
text, you end up with a trickier problem due to quoting etc. At that
point it may be better to programmatically look through all the book
elements. (There may be a way of effectively parameterising XPath
expressions, but I don't know it...)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ed_No_Spam_Please_Weber | last post by:
Hello All & Thanks in advance for your help! Background: 1) tblT_Documents is the primary parent transaction table that has 10 fields and about 250,000 rows 2) There are 9 child tables with...
4
by: Pasquale | last post by:
Hello, I wondering if there is a way to dynamically update a select list with javascript from a database query without having to reload the page to update the list?
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
3
by: Dr. Oz | last post by:
Hi, I am trying to read in a query string from one page and build a link to another page based on the query string. Here's the code I am using to read in the query string: <script...
6
by: Nicolae Fieraru | last post by:
Hi All, I have a query, Select Count(BoolField) from tblMyTable, Where BoolField = true. If I run the query by itself, it returns the number of true records I want to use the result of that...
3
by: george.lengel | last post by:
Hello experts, I have been struggling for days to solve this problem and every suggestion I find via Google does not work for me. There is probably a solution out there that will do what I want,...
0
southoz
by: southoz | last post by:
Good ay all , I'm fairly new to access(a little over 5 weeks now). Since I'v started I have picked up a lot of useful information from forums such as this and in doing so will share that information...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
8
by: babyangel43 | last post by:
Hello, I have a query set up in Access. I run it monthly, changing "date of test". I would like this query to be merged with a Word document so that the cover letter is created in Word, the fields...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.