473,395 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1479
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.