最好的方式来查询的XDocument使用LINQ? [英] Best way to query XDocument with LINQ?

查看:167
本文介绍了最好的方式来查询的XDocument使用LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一系列的项目节点,看起来像这样的XML文档:

 <数据> 
<项目>
<标签>&XYZ LT; /标签>
<描述> Lorem存有< /描述>
<参数类型=ID> 123 LT; /参数>
<参数类型=名与GT;亚当·萨维奇< /参数>
<参数类型=拉链> 90210< /参数>
< /项目>
< /数据>

和我想它LINQ到一个匿名类型是这样的:

  VAR MYDATA = 
(从根document.Root.Elements(项目)
选择新{
标签= (字符串)root.Element(标签),
说明=(字符串)root.Element(说明),
ID = ...,
名称= ...,
邮编= ...
});



什么根据其类型的值,以拉每个参数类型的最佳方式属性?由于有许多参数元素,你风与 root.Elements(参数)这是一个集合。我能想到的最好的办法是这样的通过下面的方法,但我觉得必须有一个更好的办法?



  (从C中root.Descendants(参数),其中(串)c.Attribute(类型)==ID
选择c.Value).SingleOrDefault()


解决方案

我会使用LINQ中内置的查询方法为XML,而不是XPath的。您的查询看起来好像没什么问题,但是:




  • 如果有多个项目,你需要找的,与其后裔;或者只是使用元素如果您正在寻找的项目

  • 的直接后裔你可能想拉的所有一次值,并将其转换成一个字典

  • 如果您正在使用不同的数据类型的内容,你可能想投而不是使用 .value的

  • 您可能希望创建返回匹配的XElement 给定类型的方法,而不是有几个疑问。



我个人不认为我甚至会使用一个查询表达式这一点。例如:

 静态的XElement FindParameter(的XElement元素,字符串类型)
{
返回element.Elements (参数)
.SingleOrDefault(p =>(串)p.Attribute(类型)==型);
}



然后:

  VAR MYDATA =从document.Root.Elements项目(项目)
选择新{
标签=(字符串)item.Element(标签) ,
说明=(字符串)item.Element(说明),
n =(int)的FindParameter(项目,ID),
名称=(字符串)FindParameter(项目, 名),
邮编=(字符串)FindParameter(项目,拉链)
};



我怀疑你会发现,这比使用XPath任何其他整洁,假设我已经明白了你正在尝试做的。


I have an XML document that contains a series of item nodes that look like this:

<data>
    <item>
        <label>XYZ</label>
        <description>lorem ipsum</description>
        <parameter type="id">123</parameter>
        <parameter type="name">Adam Savage</parameter>
        <parameter type="zip">90210</parameter>
    </item> 
</data>

and I want to LINQ it into an anonymous type like this:

var mydata =
    (from root in document.Root.Elements("item")
    select new {
       label = (string)root.Element("label"),
       description = (string)root.Element("description"),
       id = ...,
       name = ...,
       zip = ...
     });

What's the best way to pull each parameter type according to the value of its 'type' attribute? Since there are many parameter elements you wind up with root.Elements("parameter") which is a collection. The best way I can think to do it is like this by method below but I feel like there must be a better way?

(from c in root.Descendants("parameter") where (string)c.Attribute("type") == "id"
select c.Value).SingleOrDefault()

解决方案

I would use the built-in query methods in LINQ to XML instead of XPath. Your query looks fine to me, except that:

  • If there are multiple items, you'd need to find the descendants of that instead; or just use Element if you're looking for direct descendants of the item
  • You may want to pull all the values at once and convert them into a dictionary
  • If you're using different data types for the contents, you might want to cast the element instead of using .Value
  • You may want to create a method to return the matching XElement for a given type, instead of having several queries.

Personally I don't think I'd even use a query expression for this. For example:

static XElement FindParameter(XElement element, string type)
{
    return element.Elements("parameter")
                  .SingleOrDefault(p => (string) p.Attribute("type") == type);
}

Then:

var mydata = from item in document.Root.Elements("item")
             select new {
                 Label = (string) item.Element("label"),
                 Description = (string) item.Element("description"),
                 Id = (int) FindParameter(item, "id"),
                 Name = (string) FindParameter(item, "name"),
                 Zip = (string) FindParameter(item, "zip")
             };

I suspect you'll find that's neater than any alternative using XPath, assuming I've understood what you're trying to do.

这篇关于最好的方式来查询的XDocument使用LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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