How to query on a xml document like SQL?

A

AliRezaGoogle

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,
 
A

Arne Vajhøj

AliRezaGoogle said:
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
 
J

Jon Skeet [C# MVP]

AliRezaGoogle said:
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.
 
J

Jon Skeet [C# MVP]

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...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top