How do I get a specific node in an XmlDocument?

A

Alan Silver

Hello,

Sorry if this is an obvious question, but I'm not very familiar with
using XmlDocument, and I am struggling with a really simple task. I
couldn't find anything clear that explained what I want to do.

Suppose I have an XmlDocument like this...

<books>
<number>3</number>
<date>13 July 2006</date>
<book>
<title>Fred's big day</title>
<author>Fred Smith</author>
<instock>y</instock>
</book>
<book>
<title>Something else</title>
<author>Someone else</author>
<instock>n</instock>
</book>
<book>
<title>Big book</title>
<author>Small fellow</author>
<instock>y</instock>
</book>
</books>

I would like to list out the titles and authors, with a note as to the
availability...

"Fred's big day" by Fred Smith is in stock
"Something else" by Someone else is NOT in stock
"Big book" by Small fellow is in stock

How do I do this? TIA for any help
 
N

nate.strules

This will do the trick:

XmlDocument xml = new XmlDocument();

xml.Load(PathToXMLFile);
XmlNodeList books = xml.SelectNodes("books/book");

foreach(XmlNode book in books)
{
// for a web app, replace Console.WriteLine with Response.Write
Console.WriteLine(book.SelectSingleNode("title").InnerText);
Console.WriteLine(book.SelectSingleNode("author").InnerText);
Console.WriteLine(book.SelectSingleNode("instock").InnerText);
}
 
A

Alan Silver

XmlNodeList books = xml.SelectNodes("books/book");

Hmm, this is where it all went wrong. I tried using SelectNodes with a
path, but I never got any nodes returned.

The real XML file I'm using is much more complex than the one I showed,
and that is probably my problem. Here is a sample of the real XML
(pertinent details changed and a fair number of repeated nodes removed
for brevity). Please could you try it on this and see if you can work
out how to get an XmlNodeList of the <CoSearchItem> nodes.

Thanks

<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xsi:schemaLocation="blah" xmlns="blah" xmlns:dsig="blah" xmlns:gt="blah" xmlns:xsi="blah">
<EnvelopeVersion>1.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>NameSearch</Class>
<Qualifier>response</Qualifier>
<TransactionID>57526921</TransactionID>
<GatewayTimestamp>2006-07-13T19:35:35-00:00</GatewayTimestamp>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>removedforsecurity</SenderID>
<Authentication>
<Method>CHMD5</Method>
<Value></Value>
</Authentication>
</IDAuthentication>
<EmailAddress>removedforsecurity</EmailAddress>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys />
</GovTalkDetails>
<Body>
<NameSearch xmlns="blah" xmlns:xsi="blah" xsi:schemaLocation="blah">
<ContinuationKey>removedforsecurity</ContinuationKey>
<RegressionKey>removedforsecurity</RegressionKey>
<SearchRows>20</SearchRows>
<CoSearchItem>
<CompanyName>PIXALL LTD.</CompanyName>
<CompanyNumber>SC226107</CompanyNumber>
<DataSet>LIVE</DataSet>
<CompanyIndexStatus>DISSOLVED</CompanyIndexStatus>
<CompanyDate></CompanyDate>
</CoSearchItem>
<CoSearchItem>
<CompanyName>PIXAMEDIA LIMITED</CompanyName>
<CompanyNumber>05370464</CompanyNumber>
<DataSet>LIVE</DataSet>
<CompanyIndexStatus></CompanyIndexStatus>
<CompanyDate></CompanyDate>
</CoSearchItem>
<CoSearchItem>
<CompanyName>PIX &amp; FIX LOCKSMITHS LIMITED</CompanyName>
<CompanyNumber>05380781</CompanyNumber>
<DataSet>LIVE</DataSet>
<CompanyIndexStatus></CompanyIndexStatus>
<CompanyDate></CompanyDate>
</CoSearchItem>
</NameSearch>
</Body>
</GovTalkMessage>
 
S

sloan

This is a good tutorial that I keep handy.
It was written way back, but its not overkill for figuring out some simple
xpath statements.

http://www.microsoft.com/downloads/...6E-E7A9-4B8C-BFA6-B5BD99F336A5&displaylang=en

There is one messed up javascript function.. you gotta fix it.

the function is
function checkVersion


//This is already there
if (sVersion.search(/MSIE 5/) != -1) {
bReturn = true;
}
//ADD THIS NEW CHECK
if (sVersion.search(/MSIE 6/) != -1) {
bReturn = true;
}


It think you want


string xpath = "GovTalkMessage/Body/NameSearch/SearchRows/CoSearchItems";

XmlNodeList books = xml.SelectNodes(xpath);


xpath IS case sensitive. basically, with Xpath you have to "walk the tree".

You may have deal with the namespace issue also.

You can look here
http://www.sqlservercentral.com/col...eragingxpexcelxmlandopenxmlfordataimports.asp

for one example of working with namespaces in xml (this is the only one I
have)
this is a ~clue, not a great way of telling you how to work with it.

you're looking for
SELECT *
FROM OPENXML (@idoc,
'/ss:Workbook/ss:Worksheet[position()=1]/ss:Table/ss:Row[position()!=1]',2)
--//Remove [position()!=1] if there is no header row
WITH (
UniqueID varchar(32) './ss:Cell[position()=1]/ss:Data',
LastName varchar(32) './ss:Cell[position()=2]/ss:Data' ,
FirstName varchar(32) './ss:Cell[position()=3]/ss:Data' ,
Street varchar(32) './ss:Cell[position()=4]/ss:Data' ,
City varchar(32) './ss:Cell[position()=5]/ss:Data' ,
State varchar(2) './ss:Cell[position()=6]/ss:Data' ,
Zip varchar(12) './ss:Cell[position()=7]/ss:Data' ,
Gender char(1) './ss:Cell[position()=8]/ss:Data'

)
 
A

Alan Silver

Sloan,

Thanks for the reply, comments below...
This is a good tutorial that I keep handy.
It was written way back, but its not overkill for figuring out some simple
xpath statements.
It think you want

Is it? I haven't a clue what to do with it!!! It looks like a way of
testing functions, but without a deep knowledge of what they are or what
they are supposed to do, it lost me completely.

Thanks anyway.
string xpath = "GovTalkMessage/Body/NameSearch/SearchRows/CoSearchItems";

XmlNodeList books = xml.SelectNodes(xpath);

OK, that's exactly what I had tried, and failed, before posting. I even
cut it down to...

XmlNodeList books = xml.SelectNodes("GovTalkMessage");

but this didn't return any nodes. I have sat in the Immediate window
trying all sorts of variations, mainly along the lines of...

?xml.SelectNodes("GovTalkMessage").Count

....but always get zero returned. This is why I posted.

Any ideas what's going wrong. Even from my basic understanding of XPath
I got as far as knowing what should work, it just didn't.
xpath IS case sensitive. basically, with Xpath you have to "walk the tree".

You may have deal with the namespace issue also.

You can look here
http://www.sqlservercentral.com/columnists/sholliday/leveragingxpexcelxm
landopenxmlfordataimports.asp

I hate sites that require you to log in to read their content,
especially ones that admit in advance that you are going to get sent
e-mails you never asked for.
<snip>

Thanks for the reply. Any ideas why my XPath isn't working would be
appreciated.
 
A

Alan Silver

Anyone any ideas on this? I'm really stuck.

Alan Silver said:
OK, that's exactly what I had tried, and failed, before posting. I even
cut it down to...

XmlNodeList books = xml.SelectNodes("GovTalkMessage");

but this didn't return any nodes. I have sat in the Immediate window
trying all sorts of variations, mainly along the lines of...

?xml.SelectNodes("GovTalkMessage").Count

...but always get zero returned. This is why I posted.

Any ideas what's going wrong. Even from my basic understanding of XPath
I got as far as knowing what should work, it just didn't.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top