1

次のようなxmlファイルがあります。

<ProductGroup>
  <Product id="4601A">
    <name>Roses</name>
    <section>Floral</section>
    <price>46</price>
    <PopupImages>
      <PopupImage>img1.jpg</PopupImage>
      <PopupImage>img2.jpg</PopupImage>
    </PopupImages>
    <ImageThumbs>
      <thumb>img1-thm.jpg</thumb>
      <thumb>img2-thm.jpg</thumb>
    </ImageThumbs>
  </Product>
</ProductGroup>

本番環境では、ProductGroup ノードに多くの Product ノードが含まれる場合があります。このために、次のプロパティを持つ匿名オブジェクトのリストを作成したいと思います。

name 
section
image
thumb

XDocument を使用して Product 要素のリストを取得できます。

Dim doc As XDocument = XDocument.Load("ProductsGroups.xml")
Dim lstProducts = from x In doc Where CType(c.Element("price"), Integer) < 54

ここから私は何をしますか?

アップデート:

これをもっとよく説明しましょう。私はこれを適切に伝えたかどうか確信が持てません。

上記のxmlの例自体を取り上げます。私が書いた上記のコードは、指定された「where」条件を持つすべての製品要素を返します。返された XmlElement (製品)ごとに、n 個の匿名オブジェクトを作成する必要があります。n の数は、PopupImages ノードと ImageThumbs ノードの子の数によって異なります。ただし、私の場合、番号は同じになります。したがって、上記の例に戻ると、2 つの匿名オブジェクトが得られます。

        Anonymous1      Anonymous2
        ----------      ----------
name        Roses           Roses
section     Floral          Floral
image       img1.jpg        img2.jpg
thumb       img1-thm.jpg    img2-thm.jpg
4

2 に答える 2

1

このアプローチを試してください:

Dim query = From product In doc.Elements("Product") 
            Where Integer.Parse(product.Element("price").Value) < 54 
            Select New With
            {
                .Name = product.Element("name").Value,
                .Section = product.Element("section").Value, 
                .Images = product.Descendants("PopupImage").Select(Function(i) i.Value), 
                .Thumbs = product.Descendants("thumb").Select(Function(t) t.Value) 
            }

For Each item in query
    Console.WriteLine(item.Name)
    Console.WriteLine(item.Section)
    Console.WriteLine("Images:")
    For Each image in item.Images
        Console.WriteLine("  " + image)
    Next
    Console.WriteLine("Thumbs:")
    For Each thumb in item.Thumbs
        Console.WriteLine("  " + thumb)
    Next
Next

本当にリストが必要な場合は、呼び出しquery.ToList()て結果を変数に格納するか、元のクエリを括弧で囲んで追加しますToList()(読みやすくするために、これを行うことはお勧めしません)。同様に、現在、画像とサムネイルのタイプIEnumerable<string>は であるため、リストまたは配列が必要な場合は、適切な拡張メソッド呼び出しを追加してください。

于 2011-06-08T21:38:09.097 に答える
0

私は VB.Net に詳しくありませんが、C# では次のように記述します。

        XDocument doc = XDocument.Load("D:\\file.xml");
        var lstProducts = from XElement elem in doc.Element("ProductGroup").Elements("Product")
                          where int.Parse(elem.Element("price").Value) < 54
                          select new
                          {
                              name = elem.Element("name").Value,
                              section = elem.Element("section").Value,
                              image = elem.Element("PopupImages").Element("PopupImage").Value,
                              thumb = elem.Element("ImageThumbs").Element("thumb").Value
                          };

お役に立てれば。

編集: PopupImages と ImageThumbs のマージを処理する必要がある新しいクエリ:

       var lstProducts = from XElement elem in doc.Element("ProductGroup").Elements("Product")
                          where int.Parse(elem.Element("price").Value) < 54

                          let images = elem.Element("PopupImages").Elements("PopupImage")
                          let thumbs = elem.Element("ImageThumbs").Elements("thumb")

                          from img in images.Select(
                            (im, idx) => new KeyValuePair<string, string>(im.Value, thumbs.ElementAt(idx).Value)
                          )

                          select new
                          {
                              name = elem.Element("name").Value,
                              section = elem.Element("section").Value,
                              image = img.Key,
                              thumb = img.Value
                          };

まだ C# ですが、アイデアは明確だと思います。

于 2011-06-08T21:13:42.697 に答える