我如何获得所有“属性”?从 xml 通过 linq 到 xml
XML 示例(原始链接):
<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);
}
XML sample (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>
I'm wanting to get an instance of a class per record in the xml.
i found similar examples in here but they only had a root, then one element deep. It works, right up until i put that other element in. i want to be able to do something like
foreach(Record rec in myVar)
{
Console.WriteLine("ID: {0} User:{1} Domain:{2} LastLogon:{3}",rec.Index, rec.Username, rec.Domain, rec.LastLogon);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:使用
ToDictionary
方法更新了代码,以提高清晰度和效率。您可以尝试以下示例。如果您从
select new Record
行中删除Record
,它将导致匿名类型并且仍然有效。如果您提供了其他构造函数,您的 Record 类应该有一个默认的无参数构造函数来使用对象初始值设定项(如果您没有构造函数,它也将起作用)。否则,您可以使用可用的构造函数而不是对象初始值设定项。请注意,使用
Single()
和Value
假定 XML 格式良好且没有任何缺失元素。编辑:我已经使用
ToDictionary
方法更新了上面的代码示例,该方法更干净、更快。根据我的基准测试工作,最快的是ToDictionary
,其次是Func
,然后是Where
方法。原始查询
Query with Func
使用以下代码可以减少原始查询的冗余:
EDIT: updated code with
ToDictionary
approach for clarity and efficiency.You can try the following sample. If you remove
Record
from theselect new Record
line it will result in an anonymous type and still work. YourRecord
class should have a default parameterless constructor to use the object initializer if you have provided other constructors (it will also work if you have no constructors). Otherwise you can use the available constructors instead of the object initializer.Note that the use of
Single()
andValue
assume the XML is well formed without any missing elements.EDIT: I've updated the code sample above with the
ToDictionary
approach which is cleaner and quicker. Based on my benchmarking efforts the fastest wasToDictionary
, followed byFunc
, and then theWhere
approach.Original Query
Query with Func
Redundancy of the original query can be reduced by using the following code: