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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath Syntax question

Status
Not open for further replies.

chadau

Programmer
Nov 14, 2002
155
US
I want to get the node with username = test1. What is the xpath syntax to use in order to do this?

<UserAccounts xmlns=&quot; <User>
<UserName>Test</UserName>
<Password>pass99</Password>
<AccountHolder>
</AccountHolder>
<Address>
</Address>
<City>
</City>
<State>
</State>
<Zip>
</Zip>
<Role>
</Role>
</User>
<User>
<UserName>Test2</UserName>
<Password>pass98</Password>
<AccountHolder>
</AccountHolder>
<Address>
</Address>
<City>
</City>
<State>
</State>
<Zip>
</Zip>
<Role>
</Role>
</User>
</UserAccounts>
 
Well first you have to have a <UserName> element containing the text 'test1&quot;, then this would work
Code:
//UserName[text() = 'test1']

-pete
 
That should return the node? It doesn't seem to be working for me. Any other suggestions?
 
>> Any other suggestions?

As far as i know that's the only way. What environment and parser are you using?

-pete
 
That's what i tested with so i have no idea why it does not work for you. Posting some code might help. [peace]

-pete
 
This is what I have; code from a Global.asax file in an ASP.Net application:

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
Dim strUserName As String
Dim objRoles As XmlDocument
Dim strRoles As String()
Dim objNode As XmlNode
Dim strXPath As String

objRoles = GetRoles()

If Context.Request.IsAuthenticated Then
strUserName = Context.User.Identity.Name
strXPath = &quot;UserName[text() = '&quot; & strUserName & &quot;']&quot;
objNode = objRoles.DocumentElement.SelectSingleNode(strXPath)

*****Above: objNode gets set to nothing****

If Not IsNothing(objNode) Then
strRoles(0) = objNode.FirstChild(&quot;Role&quot;).Value
Context.User = New GenericPrincipal(Context.User.Identity, strRoles)
End If
End If
End Sub

Function GetRoles() As XmlDocument
Dim objRoles As XmlDocument

objRoles = Context.Cache(&quot;Roles&quot;)
If objRoles Is Nothing Then
objRoles = New XmlDocument()
objRoles.Load(Server.MapPath(&quot;UserAccounts/UserAccounts.xml&quot;))
Context.Cache.Insert(&quot;Roles&quot;, objRoles, New Caching.CacheDependency(Server.MapPath(&quot;UserAccounts/UserAccounts.xml&quot;)))
End If

Return objRoles
End Function
 
>> &quot;UserName[text() = '&quot; & strUserName & &quot;']&quot;

>> [red]//[/red]UserName[text() = 'test1']


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top