Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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/8/linq/3.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#_Linq_Linq To Xml - Fatal编程技术网

C# 用linq读取xml文件

C# 用linq读取xml文件,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,我有如下xml文件和设备类,我想从我的xml文件中获取列表 林克,我怎么能做到 XDocument loaded = XDocument.Load(SharedData.CONFIGURATION_FULL_PATH); var q = loaded.Descendants("device").Select(c => c); 但当然,这个代码不起作用 <?xml version="1.0" encoding="utf-8"?>

我有如下xml文件和设备类,我想从我的xml文件中获取列表 林克,我怎么能做到

            XDocument loaded = XDocument.Load(SharedData.CONFIGURATION_FULL_PATH);
            var q = loaded.Descendants("device").Select(c => c);
但当然,这个代码不起作用

<?xml version="1.0" encoding="utf-8"?>
<settings>
<device>
  <username>aa</username>
  <AgentName>aa</AgentName>
  <password>aa</password>
  <domain>aa</domain>
</device>
<device>
  <username>bb</username>
  <AgentName>bb</AgentName>
  <password>bb</password>
  <domain>bb</domain>
</device>
<device>
  <username>cc</username>
  <AgentName>cc</AgentName>
  <password>cc</password>
  <domain>cc</domain>
</device>

</settings>
您可以这样做:

var devices =
    loaded
        .Descendants("Device")
        .Select(e => new Device(
            e.Element("username").Value,
            e.Element("AgentName").Value,
            e.Element("password").Value,
            e.Element("domain").Value))
        .ToList();
您可以这样做:

var devices =
    loaded
        .Descendants("Device")
        .Select(e => new Device(
            e.Element("username").Value,
            e.Element("AgentName").Value,
            e.Element("password").Value,
            e.Element("domain").Value))
        .ToList();