首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于InnerXML的XDocument元素排序

基于InnerXML的XDocument元素排序
EN

Stack Overflow用户
提问于 2013-03-02 03:45:10
回答 4查看 1.4K关注 0票数 1

我有一个类似下面的XML文档:

代码语言:javascript
复制
<document>
    <post>
      <author>Bill Smith</author>
      <subject>Test Article</subject>
      <dates>
          <uploaded>some date</uploaded>
          <published>some date</published>
      </dates>
      <price>
           <provider>Amazon</provider>
           <cost>1540</cost>
      </price>
      <price>
           <provider>WH Smith</provider>
           <cost>2640</cost>
      </price>
    </post>
    <post>
      <author>Bill Smith</author>
      <subject>Test Article</subject>
      <dates>
          <uploaded>some date</uploaded>
          <published>some date</published>
      </dates>
      <price>
           <provider>Amazon</provider>
           <cost>1540</cost>
      </price>
      <price>
           <provider>WH Smith</provider>
           <cost>2640</cost>
      </price>
    </post> 
</document>

我使用的是带有.NET 4.5的XDocument。我知道还有其他方法可以用来排序。

我有它的工作正常拉每个帖子,并把它放入一个帖子模型。然而,我想排序价格元素,并挑选出最低的价格(以及供应商),以便我可以将其插入到我的EF数据库中。

任何帮助都会非常感谢,我完全不知道从哪里开始做这个。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-03-02 04:18:08

你可以试一试:

代码语言:javascript
复制
        XDocument doc = XDocument.Load(@"Data.xml");
        var elements = doc.Root
            .Elements("post")
            .Select(post => new
            {
                Author = post.Element("author").Value,
                Subject = post.Element("subject").Value,
                Uploaded = Convert.ToDateTime(post.Element("dates").Element("uploaded").Value),
                Published = Convert.ToDateTime(post.Element("dates").Element("published").Value),
                Price = new
                {
                    P = post
                        .Elements("price")
                        .OrderByDescending(price => Convert.ToDecimal(price.Element("cost").Value))
                        .Select(o => new
                        {
                            Provider = o.Element("provider").Value,
                            Cost = Convert.ToDecimal(o.Element("cost").Value)
                        })
                        .First()
                }
            });

        var p = elements.First();
票数 1
EN

Stack Overflow用户

发布于 2013-03-02 04:33:27

基于您提供的代码,请参见,如果此方法有效:

代码语言:javascript
复制
var xmlDocumentElement = xDocument.Load("xmlData.xml").Root;

var posts = xmlDocumentElement.Elements("post").Select(post => new 
    {
    Author = post.Element("author").Value.ToString(),
    Subject = post.Element("subject").Value.ToString(),
    AvailableDate = DateTime.Parse(post.Descendants("dates").FirstOrDefault().Value),
    Price = GetPriceFromPriceXElement(post.Elements("price").Aggregate((prev, next) => 
            Decimal.Parse(prev.Element("cost").Value.ToString()) <= Decimal.Parse(next.Element("cost").Value.ToString()) ? prev : next ))
    }
    );

public Price GetPriceFromPriceXElement(XElement price)
{
   return new Price
    { 
        Provider = price.Element("provider").Value.ToString(),
        Cost = price.Element("cost").Value.ToString()
    };
}
票数 0
EN

Stack Overflow用户

发布于 2013-03-02 05:06:00

尝尝这个。应该能行得通。一旦你有了数据,你就可以对其进行处理。请照顾NULL等。这只是围绕你的需求的基本想法。

代码语言:javascript
复制
 XDocument doc = XDocument.Load(@"XMLFile1.xml");
        var posts = doc.Root.Elements("post")
                   .Select(p =>
                            new
                            {
                               Author = p.Element("author").Value,
                               Price = p.Elements("price").OrderBy(a => decimal.Parse(a.Element("cost").Value)).First()
                     });

        foreach(var a in posts)
        {
            Console.WriteLine(a.Author + " " + a.Price);
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15165612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档