1

次のような XML ドキュメントがあります。

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

XDocument w/.NET 4.5 を使用しています。これをソートするために使用できる他の方法があることは知っています。

各投稿を引っ張って Post モデルに入れても問題ありません。ただし、EF データベースに挿入できるように、価格要素を並べ替えて最低価格 (およびプロバイダー) を選択したいと考えています。

どんな助けでも大歓迎です。私はこれからどこから始めればよいのか完全に立ち往生しています。

4

4 に答える 4

1

これを試すことができます:

        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();
于 2013-03-01T20:18:08.367 に答える
0

あなたが提供したコードに基づいて、これが機能するかどうかを確認してください:

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()
    };
}
于 2013-03-01T20:33:26.520 に答える
0

これを試して。それはうまくいくはずです。データを取得したら、それをマッサージできます。などの世話をしてくださいNULL。これは、要件に関する基本的な考え方です。

 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);
        }
于 2013-03-01T21:05:59.663 に答える