我有以下 XML:
<?xml version="1.0"?>
<Script>
<object Id="ID1">
<Expression>EXPR1</Expression>
<Result>value="0" action="sleep"</Result>
<Result>value="1" action="wakeup"</Result>
<Result>value="2" action="shutdown"</Result>
<Result>value="3" action="startup"</Result>
</object>
<object Id="ID2">
<Expression>EXPR2</Expression>
<Result>value="10" action="sleep"</Result>
<Result>value="11" action="wakeup"</Result>
<Result>value="12" action="shutdown"</Result>
<Result>value="13" action="startup"</Result>
</object>
</Script>
我正在尝试将数据抓取到这样的类:
public class Script
{
public string Id { get; set; }
public string Expression { get; set; }
public List<ScriptItem> Results { get; set; }
}
public class ScriptItem
{
public string Value { get; set; }
public string Action { get; set }
}
但我真的很困惑和混乱。
这是一个尝试示例:
var list = from item in xmlDoc.Descendants("object")
let id = item.Elements("Id")
let expression = item.Elements("Expression")
let resultlist = item.Elements("Result")
where id != null
select new Script
{
ObjectId = id.ToString(),
Expression = expression.ToString(),
Results = result.ToList()
};
我不是 Linq 和 lambda 方面的专家,所以感谢任何形式的帮助。