查找 XML 中的多个属性

发布于 2024-07-10 14:47:00 字数 529 浏览 14 评论 0原文

我正在尝试搜索 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏月而来 2024-07-17 14:47:00

要在您提供的 XML 片段中搜索您想要的内容,您需要以下 XPath 表达式:

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

如果用户名和密码匹配,则该表达式将返回某些内容,如果不匹配,则不返回某些内容。

当然,XPath 表达式只是一个字符串 - 例如,您可以使用输入表单字段的值动态构建它。

如果您告诉您所在的语言/环境,此处发布的代码示例可能会变得更加具体。

这是在 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
}

请注意,用户名和密码都不能包含单引号,否则上面的示例将失败。 以下是有关此特性的一些信息

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:

/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.

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):

// 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

音盲 2024-07-17 14:47:00

搜索 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#.

花落人断肠 2024-07-17 14:47:00

这是有关 XPath 表达式的常见问题解答。

一个或多个 XPath 表达式(其计算类型为布尔值)可以使用 连接在一起布尔运算符“and”和“or”以及使用 XPath 函数 不是()

请注意,这些名称均为小写。 XPath 区分大小写,这些名称的任何其他大写形式(例如“AND”)都不会被识别为逻辑运算符的名称。

因此,在这种特殊情况下,所需的 XPath 表达式将如下所示:

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

其中 your-ds-usernameyour-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 and your-ds-UserPassword should be substituted with the respective values you want to use from the dataset.

dawn曙光 2024-07-17 14:47:00

要在 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']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文