Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath trouble 1

Status
Not open for further replies.

EnemyGateIsDown

Technical User
Oct 3, 2002
235
GB
Hi Guys,

Im not sure whether I should post this here or on the C# forum but its really an XML issue.

I am new to XPath and am trying to find a node within my XML page which has an ID attribute of for example 'TestNode' so looking through documentation on XPath it seems that my statement should be:

[@id='TestNode']

I am running this using the C# command

xBody.SelectSingleNode(sxpath) where sxpath is a string containing the XPath statement above. However when I test this using my test page I get an error. I suspect that the problem is that my Xpath query is screwy. Can any of you XML chaps tell me what my statement needs to be in order to search all nodes within the document that have an ID attribute equal to X?

Any help is as always greatly appreciated.

Cheers,

Chris
 
>[tt][@id='TestNode'][/tt]
The first node anywhere which match the id, try this.
[tt]//*[@id='TestNode'][/tt]

 
Thanks, that seems to work without erroring but it doesnt seem to be finding my element even tho the case is the same.

Does the query above search the whole tree?

Thanks

Chris
 
>Does the query above search the whole tree?
It does.
 
Post your XML and the relevant C# code.

Jon

"I don't regret this, but I both rue and lament it.
 
Hi Guys,

My code is below however it is not finding the XML element. The element that I pass to the method definitly exists in the XML however when I step through it jumps to my else clause. My Call and the relevant part of the method are included. Any help is as always greatly appreciated.

Cheers,

Chris

XML
Code:
<tsml><head><title>Hot Desk Manager</title><meta name="display" content="screen" /><meta name="refresh" content="0" /><meta name="timeout" content="5">[URL unfurl="true"]http://bsdev-svr01/tsml/</meta></head><body><shape[/URL] type="rectangle" coords="0,0,800,600" color="#0C296B" color2="#ffffff" penwidth="1" startangle="0" sweepangle="0" gradient="none" /><font coords="10,10,780" color="#ffffff" face="arial" ID="TestFont" size="22" bold="0" italic="0" wrap="0" valign="top" align="right">11:28</font><font coords="10,250,780" color="#ffffff" face="arial" size="26" bold="0" italic="0" wrap="0" valign="top" align="center">Welcome to Hot Desk Manager</font><font coords="40,350,720" color="#ffffff" face="arial" size="18" bold="0" italic="0" wrap="1" valign="top" align="center">Click anywhere on screen to book a hot desk or amend an existing booking</font></body></tsml>

Call

Code:
	TsmlElement mylement = doc.GetElementByID ("TestID");

Method

Code:
	public TsmlElement GetElementByID (string ID)
		{
			XmlElement tmpNode;
			XmlNode tmpXmlNode;

			//string sxpath = "@id='"+ID+"'";
			//string sxpath = "attribute::*";
			//sxpath = "//title[@lang='eng']";

			string sxpath = "//*[@id='" +ID+"']";
			//string sxpath = "//[@id='TestNode']";

			tmpXmlNode = xBody.SelectSingleNode(sxpath);


			if (tmpXmlNode !=null)
			
			{
				tmpNode = (XmlElement) tmpXmlNode;
				string sName = tmpNode.Name;
				TsmlElement tmpTsmlElement = new TsmlElement(sName);
				switch(sName)// img/shape/font/valign/

..... etc
 
Hi Guys

Apologies my call didnt match my XML (different tests :)) the call should have been

Code:
TsmlElement mylement = doc.GetElementByID ("TestFont");

Chris
 
To start with, mind the case-sensitivity.
>[tt] string sxpath = "//*[@id='" +ID+"']";[/tt]
[tt] string sxpath = "//*[@[red]ID[/red]='" +ID+"']";[/tt]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top