我如何得到所有"性能"从通过的LINQ to XML XML [英] How do i get all "properties" from xml via linq to xml

查看:208
本文介绍了我如何得到所有"性能"从通过的LINQ to XML XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML示例(原文链接):

 <唱片和GT; 
<记录索引=1>
<属性名=用户名>&斯文LT; /性>
<属性名=域> infinity2< /性>
<属性名=LastLogon> 12 /二千零九分之十五< /性>
< /记录>
<记录索引=2>
<属性名=用户名>约瑟芬< /性>
<属性名=域> infinity3< /性>
<属性名=LastLogon> 01/02/2010< /性>
< /记录>
<记录索引=3>
<属性名=用户名>&羊羊LT; /性>
<属性名=域> WK-infinity9< /性>
<属性名=LastLogon> 10月2日/ 2009< /性>
< /记录>
< /记录>



我想在XML中每个记录类的一个实例。



我发现这里类似的例子,但他们只有一个根,然后一个元素很深。它的工作原理,权利,直到我把其他的因素。我希望能够做一些像

 的foreach(录音REC在myVar的)
{
Console.WriteLine(ID:{0}用户:{1}域:{2} LastLogon:{3},rec.Index,rec.Username,rec.Domain ,rec.LastLogon);
}


解决方案

编辑:更新了 ToDictionary 的效率和透明性的方法的代码。



您可以试试下面的示例。如果删除录制选择新的记录线将导致一个匿名类型依然工作。你的录制类应该有一个默认参数的构造函数使用对象初始化,如果您提供的其他构造函数(如果您有没有构造也将工作)。否则,你可以使用可用的构造函数,而不是对象初始化的。



请注意,使用单()假设XML是没有任何缺少的元素完全形成了。

  VAR XML = XElement.Parse(@<记录> 
<记录索引=1>
<属性名=用户名>斯文< /性>
<属性名=域> infinity2< /性>
<属性名=LastLogon> 12 /二千〇九分之一十五< /性>
< ; /记录>
<记录索引=2>
<属性名=用户名>约瑟芬< /性>
<属性名= 域> infinity3< /性>
<属性名=LastLogon> 01/02/2010< /性>
< /记录>
<记录索引=3>
<属性名=用户名>弗兰基< /性>
<属性名=域>周-infinity9< /性>
<属性名=LastLogon> 10月2日/ 2009< /性>
< /记录>
< /记录>);

VAR的查询=从纪录xml.Elements(记录)
让性能= record.Elements(财产)
.ToDictionary(p => p.Attribute(name)的值,p =方式> p.Value)
选择新记录
{
指数= record.Attribute (指数)。价值
用户名=属性[用户名],
区=性能[域],
LastLogon =性能[LastLogon]
};

的foreach(查询VAR REC)
{
Console.WriteLine(ID:{0}用户:{1}域:{2} LastLogon:{3 },rec.Index,rec.Username,rec.Domain,rec.LastLogon);
}

编辑:我的 ToDictionary 办法是更清洁和更快的基于我的标杆努力更新上面的代码示例最快的是 ToDictionary ,后跟函数功能,然后在其中,方法。



原始查询

  VAR的查询=从xml.Elements记录(记录)
让性能= record.Elements(财产)
选择新记录
{
指数=记录。 。属性(指数)值,
=用户名properties.Where(p => 。p.Attribute(name)的值==用户名)单()值,
区= properties.Where(P =方式> p.Attribute(名称)。价值==域)单(。)值,
LastLogon = properties.Where(p => p.Attribute(名称)。价值==LastLogon)单()值
};



查询与Func键



可以通过使用以下代码来减小原始查询的冗余:

  Func键与下;的XElement,串,串GT;的getAttribute = 
(即属性)=> e.Elements(财产)
。凡(P =方式> p.Attribute(name)的值==属性)
。单()值。

VAR的查询=从xml.Elements记录(记录)
选择新记录
{
指数= record.Attribute(指数)。价值,
用户名=的getAttribute(纪录,用户名),
区=的getAttribute(纪录,域),
LastLogon =的getAttribute(录,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);
}

解决方案

EDIT: updated code with ToDictionary approach for clarity and efficiency.

You can try the following sample. If you remove Record from the select new Record line it will result in an anonymous type and still work. Your Record 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() and Value assume the XML is well formed without any missing elements.

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);
}

EDIT: I've updated the code sample above with the ToDictionary approach which is cleaner and quicker. Based on my benchmarking efforts the fastest was ToDictionary, followed by Func, and then the Where approach.

Original Query

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
            };

Query with Func

Redundancy of the original query can be reduced by using the following code:

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")
            };

这篇关于我如何得到所有&QUOT;性能&QUOT;从通过的LINQ to XML XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆