我无法理解这段代码。
XDocument loaded = XDocument.Load(PATH);
var devices = new List<Device>(loaded.Descendants("Device").Select(e => new Device
{
UserName = "xxx",
Domain = e.Element("domain").Value,
FQDN = e.Element("fqdn").Value,
Password = e.Element("password").Value,
}));
如何添加这些元素,并获取设备列表?
答案 0 :(得分:1)
此代码使用LINQ读取XML并使用XML信息创建Device对象列表。
像这样填写字段:
你的XML是这样的:
<Devices>
<Device>
<domain>domainValue</domain>
<fqdn>fqdnValue</fqdn>
<password>passwordValue</password>
</Device>
</Devices>
答案 1 :(得分:0)
XDocument loaded = XDocument.Load(PATH);
// devices = List of device
var devices = new List<Device>
//look up for every Device in xml file
(loaded.Descendants("Device")
//create new Device object
.Select(e => new Device
{
//and fill it with found element's values
UserName = "xxx",
Domain = e.Element("domain").Value,
FQDN = e.Element("fqdn").Value,
Password = e.Element("password").Value,
}));