XML示例(original link):
<records>
<record index="1">
<property name="Username">Sven</property>
<property name="Domain">infinity2</property>
<property name="LastLogon">12/15/2009</property>
</record>
<record index="2">
<property name="Username">Josephine</property>
<property name="Domain">infinity3</property>
<property name="LastLogon">01/02/2010</property>
</record>
<record index="3">
<property name="Username">Frankie</property>
<property name="Domain">wk-infinity9</property>
<property name="LastLogon">10/02/2009</property>
</record>
</records>
我想在xml中为每个记录获取一个类的实例。
我在这里找到了类似的例子,但他们只有一个根,然后是一个元素深。它工作,直到我把其他元素放进去。我希望能够做类似
的事情foreach(Record rec in myVar)
{
Console.WriteLine("ID: {0} User:{1} Domain:{2} LastLogon:{3}",rec.Index, rec.Username, rec.Domain, rec.LastLogon);
}
答案 0 :(得分:3)
编辑:使用ToDictionary
方法更新代码,以提高清晰度和效率。
您可以尝试以下示例。如果您从Record
行中删除select new Record
,则会生成匿名类型并仍然有效。如果你提供了其他构造函数,那么你的Record
类应该有一个默认的无参数构造函数来使用对象初始值设定项(如果没有构造函数,它也可以工作)。否则,您可以使用可用的构造函数而不是对象初始值设定项。
请注意,使用Single()
和Value
假设XML格式正确,没有任何遗漏元素。
var xml = XElement.Parse(@"<records>
<record index=""1"">
<property name=""Username"">Sven</property>
<property name=""Domain"">infinity2</property>
<property name=""LastLogon"">12/15/2009</property>
</record>
<record index=""2"">
<property name=""Username"">Josephine</property>
<property name=""Domain"">infinity3</property>
<property name=""LastLogon"">01/02/2010</property>
</record>
<record index=""3"">
<property name=""Username"">Frankie</property>
<property name=""Domain"">wk-infinity9</property>
<property name=""LastLogon"">10/02/2009</property>
</record>
</records>");
var query = from record in xml.Elements("record")
let properties = record.Elements("property")
.ToDictionary(p => p.Attribute("name").Value, p => p.Value)
select new Record
{
Index = record.Attribute("index").Value,
Username = properties["Username"],
Domain = properties["Domain"],
LastLogon = properties["LastLogon"]
};
foreach(var rec in query)
{
Console.WriteLine("ID: {0} User:{1} Domain:{2} LastLogon:{3}", rec.Index, rec.Username, rec.Domain, rec.LastLogon);
}
编辑:我已使用ToDictionary
方法更新了上面的代码示例,该方法更清晰,更快捷。根据我的基准测试工作,最快的是ToDictionary
,然后是Func
,然后是Where
方法。
原始查询
var query = from record in xml.Elements("record")
let properties = record.Elements("property")
select new Record
{
Index = record.Attribute("index").Value,
Username = properties.Where(p => p.Attribute("name").Value == "Username").Single().Value,
Domain = properties.Where(p => p.Attribute("name").Value == "Domain").Single().Value,
LastLogon = properties.Where(p => p.Attribute("name").Value == "LastLogon").Single().Value
};
使用Func查询
使用以下代码可以减少原始查询的冗余:
Func<XElement, string, string> GetAttribute =
(e, property) => e.Elements("property")
.Where(p => p.Attribute("name").Value == property)
.Single().Value;
var query = from record in xml.Elements("record")
select new Record
{
Index = record.Attribute("index").Value,
Username = GetAttribute(record, "Username"),
Domain = GetAttribute(record, "Domain"),
LastLogon = GetAttribute(record, "LastLogon")
};