471,342 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 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 1429
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Ed_No_Spam_Please_Weber | last post: by
4 posts views Thread by Pasquale | last post: by
3 posts views Thread by Dr. Oz | last post: by
6 posts views Thread by Nicolae Fieraru | last post: by
3 posts views Thread by george.lengel | last post: by
reply views Thread by rosydwin | last post: by

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.