查找 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>
我需要检查“字段”中的用户名和用户密码值是否都是我与数据集值进行比较的值,是否有一种方法可以检查多个属性(AND 条件) )而不编写我自己的使用标志和打破循环的逻辑。
XMLDoc 是否有内置函数可以做到这一点? 任何帮助,将不胜感激!
I'm trying to search 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 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.
Is there a built in function of XMLDoc that does it? Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要在您提供的 XML 片段中搜索您想要的内容,您需要以下 XPath 表达式:
如果用户名和密码匹配,则该表达式将返回某些内容,如果不匹配,则不返回某些内容。
当然,XPath 表达式只是一个字符串 - 例如,您可以使用输入表单字段的值动态构建它。
如果您告诉您所在的语言/环境,此处发布的代码示例可能会变得更加具体。
这是在 C# 中执行此操作的一种方法(VB.NET 类似):
请注意,用户名和密码都不能包含单引号,否则上面的示例将失败。 以下是有关此特性的一些信息。
Microsoft 还提供了一个操作方法:如何:使用 System.Xml.XmlDocument 类执行 XPath Visual C# .NET 中的查询
To search for what you want in the snippet of XML you provided, you would need the following XPath expression:
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.
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):
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
搜索 XML 正是 XPath 的用途。 您没有指定您使用的语言,但是这里有一篇文章< /a> 使用 Java 中的 XPath 处理 XML,这里是使用 C# 的一个。
Searching XML is what XPath was made for. You didn't specify what language you're using, but here's an article on processing XML using XPath in Java, and here's one using C#.
这是有关 XPath 表达式的常见问题解答。
一个或多个 XPath 表达式(其计算类型为布尔值)可以使用 连接在一起布尔运算符“and”和“or”以及使用 XPath 函数 不是()。
请注意,这些名称均为小写。 XPath 区分大小写,这些名称的任何其他大写形式(例如“AND”)都不会被识别为逻辑运算符的名称。
因此,在这种特殊情况下,所需的 XPath 表达式将如下所示:
/*/*/field[@Username = your-ds-username and @UserPassword = your-ds-UserPassword]< /code>
其中
your-ds-username
和your-ds-UserPassword
应替换为您要从数据集中使用的相应值。This is a FAQ about XPath expressions.
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().
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.
So, in this particular case, the wanted XPath expression will be something as the following:
/*/*/field[@Username = your-ds-username and @UserPassword = your-ds-UserPassword]
where
your-ds-username
andyour-ds-UserPassword
should be substituted with the respective values you want to use from the dataset.要在 XML 标签的情况下搜索多个属性,我们可以使用以下 XPATH
/APIS/API/field[@Username='username1'][@UserPassword='password1']
To search for multiple attributes in case of XML tag, we can use the following XPATH
/APIS/API/field[@Username='username1'][@UserPassword='password1']