通过 XML 中的多个属性查找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/353843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find through multiple attributes in XML
提问by Murtaza Mandvi
I'm trying to search multiple attributes in XML :
我正在尝试在 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 in the "field" the Username AND UserPassword values are both what I am comparing with my Dataset values, is there a way where I can check multiple attributes (AND condition) without writing my own logic of using Flags and breaking out of loops.
我需要检查“字段”中的用户名和用户密码值是否都是我与我的数据集值进行比较的值,有没有一种方法可以检查多个属性(AND 条件)而无需编写自己的使用标志和破坏的逻辑出圈。
Is there a built in function of XMLDoc that does it? Any help would be appreciated!
是否有一个内置的 XMLDoc 函数可以做到这一点?任何帮助,将不胜感激!
回答by Tomalak
To search for what you want in the snippet of XML you provided, you would need the following XPath expression:
要在您提供的 XML 片段中搜索您想要的内容,您需要以下 XPath 表达式:
/APIS/API/field[@Username='username1' and @UserPassword='password1']
This would either return something, if user name and password match - or not if they don't.
如果用户名和密码匹配,这将返回一些内容 - 如果不匹配则不返回。
Of couse the XPath expression is just a string - you can build it dynamically with values that were entered into a form field, for example.
当然,XPath 表达式只是一个字符串——例如,您可以使用输入到表单字段中的值动态构建它。
If you tell what language/environment you are in, code samples posted here will likely get more specific.
如果您说出自己所处的语言/环境,则此处发布的代码示例可能会更加具体。
This is a way to do it in C# (VB.NET is analogous):
这是在 C# 中实现的一种方法(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
}
Be aware that neither user names nor passwords can contain single quotes, or the above example will fail. Here is some info on this peculiarity.
请注意,用户名和密码都不能包含单引号,否则上面的示例将失败。以下是有关此特性的一些信息。
There is also a How-To from Microsoft available: HOW TO: Use the System.Xml.XmlDocument Class to Execute XPath Queries in Visual C# .NET
还有来自 Microsoft 的 How-To 可用:HOW TO:使用 System.Xml.XmlDocument 类在 Visual C# .NET 中执行 XPath 查询
回答by Bill the Lizard
Searching XML is what XPathwas made for. You didn't specify what language you're using, but here's an articleon processing XML using XPath in Java, and here's oneusing C#.
搜索 XML 是XPath 的用途。你没有指定你所使用的语言,但这里的一篇文章上处理在Java中使用XPath XML,并且这里是一个用C#。
回答by Dimitre Novatchev
This is a FAQ about XPathexpressions.
这是一个关于XPath表达式的 FAQ。
One or more XPath expressions (whose evaluated type is boolean), can be connected together using the boolean operators"and" and "or" and using the XPath function not().
一个或多个 XPath 表达式(其计算类型为布尔值)可以使用布尔运算符“and”和“or”以及使用 XPath 函数not()连接在一起。
Do note that the names of these are all in lower case. XPath is case-sensitive and a any other capitalization of these names (such as "AND") will not be recognized as the name of the logical operators.
请注意,这些名称都是小写的。XPath 区分大小写,并且这些名称的任何其他大小写(例如“AND”)都不会被识别为逻辑运算符的名称。
So, in this particular case, the wanted XPath expression will be something as the following:
因此,在这种特殊情况下,所需的 XPath 表达式将如下所示:
/*/*/field[@Username = your-ds-username and @UserPassword = your-ds-UserPassword]
/*/*/field[@Username = your-ds-username and @UserPassword = your-ds-UserPassword]
where your-ds-usernameand your-ds-UserPasswordshould be substituted with the respective values you want to use from the dataset.
whereyour-ds-username和 your-ds-UserPassword应该替换为您要从数据集中使用的相应值。
回答by Dhanesh Kumar
To search for multiple attributes in case of XML tag, we can use the following XPATH /APIS/API/field[@Username='username1'][@UserPassword='password1']
要在 XML 标记的情况下搜索多个属性,我们可以使用以下 XPATH /APIS/API/field[@Username='username1'][@UserPassword='password1']

