2016-07-01 10 views
0

私は特定のXMLを読み取ろうとしていますが、必要な情報を2つの異なるクラスOrderOrderDetailに保存しようとしています。私は次のコードで私のXMLから「BuyerID」をお読みしようとすると、しかし、それはReadElementContentAsString method is not supported on node type None. Line 1, Position 634で、私にInvalidOperationExceptionをスロー:http://pastebin.com/Lu4mKtwq、この特定の行でC#XmlReader ReadElementContentAsString()InvalidOperationException

order.CustomerID = reader.ReadElementContentAsString(); 

私のソースコード:http://pastebin.com/JyTz8x0G

これは、私が働いているXMLです:テキストで

Order xml that contains information for order and order detail class XML:

<Orders> 
<Order ID="O2"> 
<OrderDate>1/7/2016</OrderDate> 
<BuyerID>WSC1810</BuyerID> 
<OrderItem> 
<Item ID="R1"> 
<ItemName>8GB RAM King</ItemName> 
<Decscription>8GB RAM King</Decscription> 
<Capacity>8GB</Capacity> 
<Quantities>150</Quantities> 
<existingUnitPrice>100.00</existingUnitPrice> 
</Item> 
<Item ID="R2"> 
<ItemName>4GB RAM King</ItemName> 
<Decscription>4GB RAM King Brand</Decscription> 
<Capacity>4GB</Capacity> 
<Quantities>100</Quantities> 
<existingUnitPrice>50.00</existingUnitPrice> 
</Item> 
</OrderItem> 
<RemarksandSpecialInstruction>Fragile, handle with care</RemarksandSpecialInstruction> 
</Order> 
</Orders> 

私が働いているクラス: Order and Order detail classes

+0

画像の代わりにXML入力をテキストとしても投稿できますか? – cvraman

+0

また、好奇心から、XMLをオブジェクトに変換することを意図している場合、逆もまたあります。すべてのプロパティを読み込むのではなく、シリアライゼーションと逆シリアル化を使用するのはなぜですか? – cvraman

+0

プロジェクトの仕様:v – Kei

答えて

1

ReadElementContentAsString方法の結果を破棄しないことによって、あなたのコードを向上させることができます。 は、だからあなたの場合には以下のコード

reader.ReadToFollowing("OrderDate"); 
order.OrderDate = reader.ReadElementContentAsString(); 

を持った後、今のコードでは、受注時に既にあるので、再びそれを読んしようとしないでください。 は、代わりに以下のコードのようなものを実行します。

 while (reader.ReadToFollowing("Order")) 
     {    
      order.OrderID = reader.GetAttribute("ID"); 
      string orderID = reader.Value; 

      reader.ReadToFollowing("OrderDate"); 
      if(reader.Name.Equals("OrderDate")) 
       order.OrderDate = reader.ReadElementContentAsString(); 
      if (reader.Name.Equals("BuyerID")) 
       order.CustomerID = reader.ReadElementContentAsString();    
      orderList.Add(order); 
      while (reader.ReadToFollowing("Item")) 
      { 
       OrderDetail i = new OrderDetail(); 
       i.OrderID = orderID;     
       i.ItemID = reader.GetAttribute("ID"); 

       reader.ReadToFollowing("Decscription"); 
       if (reader.Name.Equals("Decscription")) 
        i.Description = reader.ReadElementContentAsString(); 
       if (reader.Name.Equals("Capacity")) 
        i.Capacity = reader.ReadElementContentAsString(); 
       if (reader.Name.Equals("Quantities")) 
        i.Quantity = reader.ReadElementContentAsInt(); 
       if (reader.Name.Equals("existingUnitPrice")) 
        i.AskingPrice = reader.ReadElementContentAsDecimal(); 
       orderDetailList.Add(i); 
      } 
     } 

はまた、あなたのxmlとあなたのモデルが一致していることを確認してください。

0

顧客IDが実際に正しく読み込まれている、それは例外を上げていること下の行です。 あなたのエラーのコード行にある:

reader.ReadToFollowing("Instructions"); 

XML要素Instructionsが含まれていないことを理由。 あなたは、次のいずれかに現在のノードの動きを読んだ後ReadToFollowing

if (reader.ReadToFollowing("Instructions")) 
    reader.ReadElementContentAsString(); 
+0

正しい要素名に変更しましたが、それでも同じ場所を指しています – Kei

関連する問題