Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用XmlDocument函数获取索引?C#_C#_Xml_Webforms - Fatal编程技术网

如何使用XmlDocument函数获取索引?C#

如何使用XmlDocument函数获取索引?C#,c#,xml,webforms,C#,Xml,Webforms,我创建了一个C#Web表单应用程序,希望从特定对象获取索引。这就是我现在所拥有的。在这个练习中,我想输入登录名和密码,以便从那个特定的人那里获取信息。允许重复的名称/密码。我能想到的是从节点获取索引,然后使用XmlNodeList info=reader.GetElementsByTagName(“info”)和textBox1.Text=root.GetElementsByTagName(“信息”)[index].InnerText我需要找到那个特定的索引,但我对此感到有点困惑。希望有人能给我

我创建了一个C#Web表单应用程序,希望从特定对象获取索引。这就是我现在所拥有的。在这个练习中,我想输入登录名和密码,以便从那个特定的人那里获取信息。允许重复的名称/密码。我能想到的是从节点获取索引,然后使用
XmlNodeList info=reader.GetElementsByTagName(“info”)
textBox1.Text=root.GetElementsByTagName(“信息”)[index].InnerText我需要找到那个特定的索引,但我对此感到有点困惑。希望有人能给我任何建议,以及如何做的新想法,谢谢

private int GetIndex(string loginname, string password)
{
    reader = new XmlDocument();
    reader.Load(@filepath);
    XmlNodeList loginN = reader.GetElementsByTagName("Loginname");
    XmlNodeList Pass = reader.GetElementsByTagName("Password");
}
XML:


管理
232
你好,我是第一人称
管理
4445
你好,我是第二人称

如果您想通过给定的
Loginname
密码获取
信息
,那么可以使用XPath

string username = "Admin";
string password = "232";
string xPathQuery = 
    String.Format("Authentication/User[Loginname='{0}' and Password='{1}']",
    username, password);

XmlNode node = reader.SelectSingleNode(xPathQuery);    

if (node == null)
{
    // Incorrect username \ password
} else {
    string info = node.SelectSingleNode("User/Info").InnerText;
    textBox1.Text = info;
}
请注意,如果有多个
用户
使用相同的用户名和密码-
选择SingleNode
将首先选择一个。

类似如下:

XmlDocument doc = new XmlDocument();
doc.Load("XmlFile1.xml");
var node = doc.SelectSingleNode("Authentication/User[Loginname='Admin' and Password='4445']");

var childnodes = doc.DocumentElement.ChildNodes;
for (int i = 0; i < childnodes.Count; i++)
{
    if (node == childnodes[i])
    {
        Console.WriteLine("INDEX IS: " + i);
    }
}
XmlDocument doc=新的XmlDocument();
doc.Load(“XmlFile1.xml”);
var node=doc.SelectSingleNode(“身份验证/用户[Loginname='Admin'和Password='4445']”);
var childnodes=doc.DocumentElement.childnodes;
for(int i=0;i
使用.SelectNodes()可以通过XPath查询字符串快速找到节点。我不知道有什么方法可以获得节点索引。如果使用SelectNodes获取XmlNodes,则使用for循环遍历Authenticaton的所有子级,并查找与找到的子级匹配的XmlNodes。那么您就可以使用索引了。@Wolf5您可以给我一个例子吗?我已经厌倦了您的格式,但是
XmlNode node=reader.SelectSingleNode(xPathQuery)给了我空值。但是,我输入了正确的用户名和密码。@YeldarKurmangaliyev@har07我发现了一个错误,
String.Format(“身份验证/用户[Loginname='{0}'和密码='{1}',用户名和密码”)
需要替换为
String.Format(//User[Loginname='{0}'和Password='{1}]],username,Password)。不知道为什么,但它的工作!谢谢大家:)
XmlDocument doc = new XmlDocument();
doc.Load("XmlFile1.xml");
var node = doc.SelectSingleNode("Authentication/User[Loginname='Admin' and Password='4445']");

var childnodes = doc.DocumentElement.ChildNodes;
for (int i = 0; i < childnodes.Count; i++)
{
    if (node == childnodes[i])
    {
        Console.WriteLine("INDEX IS: " + i);
    }
}