Search through multiple attributes in XML

I am trying to search for multiple attributes in XML:

<APIS> <API Key="00001"> <field Username="username1" UserPassword="password1" FileName="Filename1.xml"/> <field Username="username2" UserPassword="password2" FileName="Filename2.xml"/> <field Username="username3" UserPassword="password3" FileName="Filename3.xml"/> </API> </APIS> 

I need to check if the Username and UserPassword values โ€‹โ€‹correspond to the Username and UserPassword values โ€‹โ€‹to my Dataset values, is there a way to check multiple attributes (AND condition) without writing my own logic for using Flags and loop loops.

Is there a built-in XMLDoc function that does this? Any help would be appreciated!

+7
xml search xml-attribute
source share
4 answers

To find the fragment of an XML document that you need, you need the following XPath expression:

 /APIS/API/field[@Username='username1' and @UserPassword='password1'] 

This will either return something if the username and password match - or not if they do not.

An XPath expression is just a string - you can build it dynamically with the values โ€‹โ€‹that were entered in the form field, for example.

If you tell us which language / environment you are in, the code samples posted here are likely to become more specific.

This is the way to do it in C # (similar to VB.NET):

 // make sure the following line is included in your class using System.Xml; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load("your XML string or file"); string xpath = "/APIS/API/field[@Username='{0}' and @UserPassword='{1}']"; string username = "username1"; string password = "password1"; xpath = String.Format(xpath, username, password); XmlNode userNode = xmldoc.SelectSingleNode(xpath); if (userNode != null) { // found something with given user name and password } else { // username or password incorrect } 

Remember that neither usernames nor passwords can contain single quotes, otherwise the above example will fail. Here is some information about this feature .

There is also a Microsoft How-To: HOW: Use the System.Xml.XmlDocument class to execute XPath queries in Visual C # .NET

+25
source share

XML search is XPath . You did not indicate which language you are using, but here is an article on XML processing using XPath in Java and here is one using C #.

+2
source share

This is a FAQ on XPath Expressions .

One or more XPath expressions (whose evaluated type is logical) can be linked using the logical operators "and" and "or" and using the XPath not () function.

Please note that the names of these files are lowercase . XPath is case sensitive and any other capitalization of these names (such as "AND") will not be recognized as the name of logical operators.

So, in this particular case, the required XPath expression will look like this:

/*/*/field[@Username = your-ds-username and @UserPassword = your-ds-UserPassword]

where your-ds-username and your-ds-UserPassword should be replaced with the corresponding values โ€‹โ€‹that you want to use from the dataset.

+1
source share

To search for multiple attributes in the case of an XML tag, we can use the following XPATH / APIS / API / field [@Username = 'username1'] [@UserPassword = 'password1']

0
source share

All Articles