Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
C# 使用LINQ在XML文件中搜索两个不同的元素_C#_Xml_Linq - Fatal编程技术网

C# 使用LINQ在XML文件中搜索两个不同的元素

C# 使用LINQ在XML文件中搜索两个不同的元素,c#,xml,linq,C#,Xml,Linq,我想使用下面的linq搜索,但要修改它,以便它也可以在用户和管理员中搜索 NAME、ID和PASS是用于比较的字符串。如果所有3个匹配,那么程序知道它是什么类型的用户,管理员还是用户,并从那里开始移动。否则,他不在任何列表中,将显示一个错误 XElement xelement = XElement.Load(@"c:\user.xml"); IEnumerable<XElement> users = xelement.Elements(); foreach (var user in

我想使用下面的linq搜索,但要修改它,以便它也可以在用户和管理员中搜索

NAME
ID
PASS
是用于比较的字符串。如果所有3个匹配,那么程序知道它是什么类型的用户,管理员还是用户,并从那里开始移动。否则,他不在任何列表中,将显示一个错误

XElement xelement = XElement.Load(@"c:\user.xml");
IEnumerable<XElement> users = xelement.Elements();
foreach (var user in users)
{
  if((user.Element("Id").Value==ID)&&(user.Element("Username").Value==NAME)&&(user.Element("Password").Value==PASS))

}
XElement-XElement=XElement.Load(@“c:\user.xml”);
IEnumerable users=xelement.Elements();
foreach(用户中的var用户)
{
if((user.Element(“Id”).Value==Id)&&(user.Element(“Username”).Value==NAME)&&(user.Element(“Password”).Value==PASS))
}
xml文件的构建方式如下:

<Data>    
    <UserList>
        <User Id="123" Username="abc" Password="abc123"></User>
    </UserList>
    <AdminList>
        <Admin Id="123" Username="abc" Password="abc123"></Admin>
    </AdminList>
</Data>    

您当前的代码使用LINQ到XML类,但实际上没有使用任何LINQ查询

您可以做的是:

  • 分别获取管理员和用户

    XDocument xDoc = XDocument.Load(@"c:\user.xml");
    var admins = xDoc.Root.Element("AdminList").Elements("Admin");
    var users = xDoc.Root.Element("UserList").Elements("User");
    
  • 将它们连接在一起:

    var adminsAndUsers
        = admins.Select(x => new { Element = x, Type = "Admin" })
                .Concat(users.Select(x => new { Element = x, Type = "User" }));
    
  • 查询结果集合以查找匹配的用户。我使用了
    (string)XAttribute
    强制转换而不是
    XAttribute.Value
    属性,因为它使用起来更安全(当属性不存在时不会引发异常)

  • 检查查询结果

    if(user != null)
    {
        // user is there
        var type = user.Type;
    }
    else
    {
        // no user matches
    }
    
  • 试试这个解决方案

    结构

     public struct test
            {
                public bool isStudent;
                public string id;
                public string Username;
                public string Password;
            }
    
    
    
    List<test>  users = doc.Descendants("User").Where(e => e.Attribute("Id").Value == "123").Select(e => new test{ isStudent = true, id = e.Attribute("Id").Value, Username = e.Attribute("Username").Value, Password = e.Attribute("Username").Value }).ToList();
                  List<test>  admins = doc.Descendants("Admin").Where(e => e.Attribute("Id").Value == "123").Select(e => new test { isStudent = false, id = e.Attribute("Id").Value, Username = e.Attribute("Username").Value, Password = e.Attribute("Username").Value }).ToList();
                List<test> allTogether = users.Concat(admins).ToList();
                foreach (var xElement in allTogether)
                {
                   //check if xElement property isStudent is true
                }
    
    公共结构测试
    {
    公立学校是学生;
    公共字符串id;
    公共字符串用户名;
    公共字符串密码;
    }
    列出users=doc.substands(“User”)。其中(e=>e.Attribute(“Id”)。Value==“123”)。选择(e=>newtest{isStudent=true,Id=e.Attribute(“Id”)。Value,Username=e.Attribute(“Username”)。Value,Password=e.Attribute(“Username”).Value})。ToList();
    列出admins=doc.substands(“Admin”)。其中(e=>e.Attribute(“Id”)。Value==“123”)。选择(e=>newtest{isStudent=false,Id=e.Attribute(“Id”)。Value,Username=e.Attribute(“Username”)。Value,Password=e.Attribute(“Username”).Value})。ToList();
    List allother=users.Concat(admins.ToList();
    foreach(组合中的变量xElement)
    {
    //检查xElement属性isStudent是否为true
    }
    

    现在,在allTogether列表中,您可以检查所需的avery属性。

    这是您的完整xml吗?@EhsanSajjad我编辑了它,但或多或少是这样,我使用xml文件存储数据,其中还有其他信息,我只想搜索管理员和用户“列表”要查找它是否与我的查询匹配,您使用了错误的
    运算符。如果((user.Element(“Id”).Value==Id)和(&&(user.Element(“用户名”).Value==NAME)和(&(user.Element(“密码”).Value==PASS))`@Tim thx在写入时出错,请使用
    &&&
    ,而不是
    &
    。问题仍然存在,当XElement加载所有数据时,如何让他通过,您编写的查询将不会像您预期的那样工作。首先,您要查询文档中的所有元素,其次,
    Id
    Username
    Password
    都是属性,而不是元素。因此,它将两者合并为一个属性。不要获取“X=>”,虽然它确实会发现是否有用户,但如何知道它是管理员还是用户?我如何知道是哪种用户?你能解释一下“x=>”部分吗?现在我明白了,编辑的时候用thx。我怎么才能分辨出哪个是哪个?用户还是管理员?这是一种语法。让我编辑我的解决方案,以包含某种类型的用户。只是小东西第1节“ArrayList”?你是说“用户列表”吗?
     public struct test
            {
                public bool isStudent;
                public string id;
                public string Username;
                public string Password;
            }
    
    
    
    List<test>  users = doc.Descendants("User").Where(e => e.Attribute("Id").Value == "123").Select(e => new test{ isStudent = true, id = e.Attribute("Id").Value, Username = e.Attribute("Username").Value, Password = e.Attribute("Username").Value }).ToList();
                  List<test>  admins = doc.Descendants("Admin").Where(e => e.Attribute("Id").Value == "123").Select(e => new test { isStudent = false, id = e.Attribute("Id").Value, Username = e.Attribute("Username").Value, Password = e.Attribute("Username").Value }).ToList();
                List<test> allTogether = users.Concat(admins).ToList();
                foreach (var xElement in allTogether)
                {
                   //check if xElement property isStudent is true
                }