Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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/9/delphi/9.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# 在c中从xml查询中获取对象值_C#_Xml_Linq_Object - Fatal编程技术网

C# 在c中从xml查询中获取对象值

C# 在c中从xml查询中获取对象值,c#,xml,linq,object,C#,Xml,Linq,Object,我找不到如何从对象中检索值。代码如下: private object GetUserData(XElement xmlDoc) { return xmlDoc.Descendants("UserData").Select(u => new { UserName = u.Element("UserName").Value, Pass

我找不到如何从对象中检索值。代码如下:

    private object GetUserData(XElement xmlDoc)
    {
        return
              xmlDoc.Descendants("UserData").Select(u => new
              {
                  UserName = u.Element("UserName").Value,
                  Pass = u.Element("Pass").Value,
                  CurTemplate = u.Element("CurrentTemplate").Value
              });
    }
这将存储在:

   var userData = GetUserData(xmlDoc);
当我调试并打开userData值时,它有一个列表,其中包含我定义的属性,但我无法实现它们。我试图将返回值转换为列表、字典、查找、字符串等,但这根本没有帮助。有人能帮忙吗


谢谢大家!

您不能返回匿名类型,而通过返回对象来假装匿名类型对这一点毫无帮助。如果您需要在定义匿名对象的范围之外的属性,那么匿名对象不是您所需要的,您需要定义一个具有所需属性的类:UserName、Pass、CurTemplate,并选择该类型的新实例,而不是新的匿名类型。然后,您将能够从方法中返回IEnumerable

public class UserData
{
     public string UserName;
     public string Pass;
     public string CurTemplate;
}

private IEnumerable<UserData> GetUserData(XElement xmlDoc)
{
    return
          xmlDoc.Descendants("UserData").Select(u => new UserData
          {
              UserName = u.Element("UserName").Value,
              Pass = u.Element("Pass").Value,
              CurTemplate = u.Element("CurrentTemplate").Value
          });
}

您不能返回匿名类型,而通过返回对象来假装匿名类型也不会有任何帮助。如果您需要在定义匿名对象的范围之外的属性,那么匿名对象不是您所需要的,您需要定义一个具有所需属性的类:UserName、Pass、CurTemplate,并选择该类型的新实例,而不是新的匿名类型。然后,您将能够从方法中返回IEnumerable

public class UserData
{
     public string UserName;
     public string Pass;
     public string CurTemplate;
}

private IEnumerable<UserData> GetUserData(XElement xmlDoc)
{
    return
          xmlDoc.Descendants("UserData").Select(u => new UserData
          {
              UserName = u.Element("UserName").Value,
              Pass = u.Element("Pass").Value,
              CurTemplate = u.Element("CurrentTemplate").Value
          });
}

这很有效。感谢您的解决方案和澄清!这很有效。感谢您的解决方案和澄清!