2

私は次のxml形式を持っています

<feed>
 <entry>
   <s:product>
   <s:author>
    <s:name>amazone</s:name>
    <s:accountId>1111</s:accountId>
   </s:author>
   <s:title>xxx
   </s:title>
   <s:condition>used
   </s:condition>
   <s:inventories>
    <s:inventory channel="online" availability="inStock">
     <s:price shipping="4.9" currency="EUR">14.95</s:price>
    </s:inventory>
   </s:inventories>
  </s:product>
 </entry>
 <entry>
  <s:product>
   <s:author>
    <s:name>ebay</s:name>
    <s:accountId>1221</s:accountId>
   </s:author>
   <s:title>yyy
   </s:title>
   <s:condition>new
   </s:condition>
   <s:inventories>
    <s:inventory channel="online" availability="inStock">
     <s:price shipping="4.90" currency="EUR">99.95</s:price>
    </s:inventory>
   </s:inventories>
  </s:product>
 </entry>  
</feed>

次のようなデータテーブルに入れようとしています

name       condition       price      shipping      totalprice      availablity
amazone    used            14.95      4.95          19.90           inStock 
ebay       new             99.95      4.90          104.85          inStock 

ご覧のとおり、それぞれ<entry>が行であると想定されており、すべてのタグの値は必要ありません。次のとおりです

column name       :value of <s:name>      
column condition  :value of <s:condition> 
column sprice     :value of <s:price>
column shipping   :value of shipping which is an attribute of <s:price>
column totalprice :value of <s:price> + its attribute shipping
column avilability:value of avialability an attribute of <s:inventory>

LINQを使用してこれを達成するにはどうすればよいですか?

4

1 に答える 1

6

あなたのコードでは多くのことが行われていますが、そのほとんどは良くありません。LINQ to XML ライブラリを適切に使用していないだけです。

  • XML ドキュメントと関心のあるデータには名前空間があります。ドキュメントで名前空間の宣言を省略したため、どの要素にも適切にアクセスできません。

    XNamespaceドキュメントで使用される名前空間に設定された変数を作成し、適切な要素を参照しようとするときにそれを使用する必要があります。

  • 要素/属性 (文字列) の値をそのような double にキャストすることはできません。その場合は解析する必要があります。ただし、LINQ to XMLl は、変換を処理する明示的なキャストを提供します。したがって、含まれる値ではなく、要素/属性自体の適切な型にキャストします。を操作する代わりにこれを使用しValueます。

    ちなみに、通貨を扱うときはいつでもdouble(または他の浮動小数点型) を使用しないでdecimalください。

  • データ テーブルからインスタンスを作成せずに、データ行のインスタンスを作成することはできません。クエリで最初にデータに関心を持つようにすることをお勧めします。テーブルの新しいデータ行の作成 データを取得したら、次に行の作成とデータの入力について考えます。

そうは言っても、より適切に使用する方法は次のとおりです。

XDocument doc = GetDocument();
// assuming this is the correct namespace
XNamespace s = "http://www.google.com/shopping/api/schemas/2010";
var query =
    from product in doc.Root.Elements("entry").Elements(s + "product")
    // declare some variables to make the query cleaner
    let inventory = product.Element(s + "inventories").Element(s + "inventory")
    let price = (decimal)inventory.Element(s + "price")
    let shipping = (decimal)inventory.Element(s + "price").Attribute("shipping")
    select new
    {
        Name = (string)product.Element(s + "author").Element(s + "name"),
        Condition = (string)product.Element(s + "condition"),
        Price = price,
        Shipping = shipping,
        TotalPrice = price + shipping,
        Availability = (string)inventory.Attribute("availability"),
    };

var dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Condition", typeof(string));
dt.Columns.Add("Price", typeof(decimal));
dt.Columns.Add("Shipping", typeof(decimal));
dt.Columns.Add("TotalPrice", typeof(decimal));
dt.Columns.Add("Availability", typeof(string));
foreach (var product in query)
{
    dt.Rows.Add(
        product.Name,
        product.Condition,
        product.Price,
        product.Shipping,
        product.TotalPrice,
        product.Availability
    );
}
于 2013-03-04T00:25:28.930 に答える