2017-11-03 9 views
0

XMLファイルを作成するために、繰り返し処理してデータを取り出すために必要なオブジェクトのコレクションがあります。私はXML Linqを使用しようとしていますが、私はその概念を把握していないようです。XMLからLinqを使用してICollectionを反復する

<?xml version="1.0" encoding="utf-8"?> 
<Feed xmlns="http://www.bazaarvoice.com/xs/PRR/ProductFeed/14.7" name="LifetimeProducts" incremental="false" extractDate="2017-10-20T13:21:41"> 

    <Products> 
     <Product> 
      <ExternalId>12345</ExternalId> 
      <Name>Product 1</Name> 
           <Description> 
       <![CDATA[Proudct Description]]></Description> 
      <BrandExternalId>Brandx</BrandExternalId> 
      <CategoryExternalId>Category</CategoryExternalId> 
           <ProductPageUrl><![CDATA[http://something.com]]></ProductPageUrl> 
       <ImageUrl>http://image.com</ImageUrl> 
      <ModelNumbers> 
       <ModelNumber>12345</ModelNumber> 
      </ModelNumbers> 
      <ManufacturerPartNumbers> 
       <ManufacturerPartNumber>12345</ManufacturerPartNumber> 
      </ManufacturerPartNumbers> 
      <Attributes> 
       <Attribute id="BV_FE_FAMILY"> 
        <Value>Family</Value> 
       </Attribute> 
            <Attribute id="BV_FE_FAMILY"> 
        <Value>Family2</Value> 
       </Attribute> 
            <Attribute id="BV_FE_FAMILY"> 
        <Value>Family 3</Value> 
       </Attribute> 
      </Attributes> 
     </Product> 
    </Products> 

私は(rootとして)一つだけの供給ノードを必要として、下の製品のノードを持っている:私は、次のXMLを作成しようとしています

string jSonProducts = File.ReadAllText(settings.productJsonConfig.JSONProductFilePath); 

     ICollection<ProductSearchModel> prods = null; 
     prods = JsonConvert.DeserializeObject<ICollection<ProductSearchModel>>(jSonProducts); 

     foreach (ProductSearchModel prod in prods) 
     { 
      var xmlNode = 
      new XElement("Feed", 
       new XAttribute("xmlns", settings.bvXMLConfig.xmlns), 
       new XAttribute("name", settings.bvXMLConfig.xmlName), 
       new XAttribute("incrmental", settings.bvXMLConfig.xmlIncremental), 
       new XAttribute("extractDate",DateTime.UtcNow), 
       new XElement("Products"), 
       new XElement("Product"), 
        new XElement("ExternalId", prod.SKU), 
        new XElement("Name", prod.Description.Name), 
        new XElement("Description", prod.Description.Description), 
        new XElement("BrandExternalID",prod.Properties.Brand.FeedName), 
        new XElement("CategoryExternalId"), //look up the category here by sku 
        new XElement("ModelNumbers"), 
         new XElement("ModelNumber",prod.SKU), 
        new XElement("ManufacturingPartNumbers"), 
         new XElement("ManufacturingPartNumber",prod.SKU), 
        new XElement("UPCs"), 
         new XElement("UPC", prod.UPC), 
        new XElement("Attributes"), 
        new XElement("Attribute"), 
         new XAttribute("id","BV_FE_FAMILY"), 
         new XElement("Value") 
       ); 

     } 

:ここに私のコードですこれです。これは動作していないようで、これはLINQとXMLで実際に作業している初めてのことです。コードはノードにデータを引き込むように見えますが、foreachループではこれが間違っていると思います。これを得るためには何を調整する必要がありますか?

ありがとうございました。

+0

prodには、このXMLを作成するために必要以上に多くのデータが含まれているため、prodをシリアル化するだけで動作します。私はprodデータをこれらのXMLタグのセットにプッシュする必要があります。 – john

+1

C#ではインデントが重要ではありません。パラメータを渡すメソッド呼び出しに相対的にインデントすることによって、パラメータを渡すことはありません。あなたはそれを括弧の中に入れて渡します。 –

答えて

0

C#では、メソッドに引数をかっこ内に入れて渡します。たとえば、これは動作しません。

double sqrt = Math.Sqrt(), 
        1234; 

あなたはこのようにそれを実行します。

double sqrt = Math.Sqrt(1234); 

また、コレクション内のすべての項目に対してすべてやり直す新しいルートノードを作成しようとしています。代わりに、複数の子を含むルートノードが必要です。

あなたのコードはおそらくこのように見えるはずです。私はあなたの設定やあなたのProductSearchModelクラスを持っていないので、私はこれをテストしていないことに留意してください。私はあなたのインデントが少なくともあなたの意図を反映しているという前提に依拠しており、要素の名前(ModelNumber/ModelNumberなど)はそれを裏付けているようです。

一見したところでループの邪魔はあなたのコードに似ていますが、かっこには細心の注意を払ってください。彼らは世界のすべての違いを作ります。

var xFeed = 
    new XElement("Feed", 
     new XAttribute("xmlns", settings.bvXMLConfig.xmlns), 
     new XAttribute("name", settings.bvXMLConfig.xmlName), 
     new XAttribute("incrmental", settings.bvXMLConfig.xmlIncremental), 
     new XAttribute("extractDate", DateTime.UtcNow) 
    ); 

var xProducts = new XElement("Products"); 
xFeed.Add(xProducts); 

foreach (ProductSearchModel prod in prods) 
{ 
    var xProduct = 
     new XElement("Product", 
      new XElement("ExternalId", prod.SKU), 
      new XElement("Name", prod.Description.Name), 
      new XElement("Description", prod.Description.Description), 
      new XElement("BrandExternalID", prod.Properties.Brand.FeedName), 
      new XElement("CategoryExternalId"), //look up the category here by sku 
      new XElement("ModelNumbers", 
       new XElement("ModelNumber", prod.SKU)), 
      new XElement("ManufacturingPartNumbers", 
       new XElement("ManufacturingPartNumber", prod.SKU)), 
      new XElement("UPCs", 
       new XElement("UPC", prod.UPC)), 
      new XElement("Attributes"), 
      new XElement("Attribute", 
       new XAttribute("id", "BV_FE_FAMILY"), 
       new XElement("Value")) 
     ); 
    xProducts.Add(xProduct); 
} 
+1

ありがとう、これは私のための素晴らしい出発点です。上記のコメントに基づいてかっこを使って作業していました。 – john

+0

xFeed.Save([path])を使用してこのファイルを保存しようとすると、次のエラーが表示されます。 {"接頭辞 ''を ''から 'http://www.bazaarvoice.com/xs/ PRR/ProductFeed/14.7 'と同じ開始タグ内にあります。 "} – john

+0

@johnこの回答のようにXNamespaceを与える必要があります:https://stackoverflow.com/a/11023060/424129 –

関連する問題