-1

I'm trying to read a particular XML, save the necessary information into two different classes Order and OrderDetail. However, when I try to read "BuyerID" from my XML with the following code, it throws me an InvalidOperationException, with The ReadElementContentAsString method is not supported on node type None. Line 1, Position 634: http://pastebin.com/Lu4mKtwq

At this particular line:

order.CustomerID = reader.ReadElementContentAsString();

My source code: http://pastebin.com/JyTz8x0G

This is the XML that I am working with:

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

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

The classes that I'm working with: Order and Order detail classes

Kei
  • 315
  • 1
  • 4
  • 18
  • 1
    Can you post the XML Input also as a text , instead of an image ? – cvraman Jul 01 '16 at 05:10
  • Also out of curiosity, if your intent is to convert XML into object and vice versa why not simply use serialization and deserialization instead of reading every property ? – cvraman Jul 01 '16 at 05:42

2 Answers2

2

The ReadElementContentAsString method after reading the current node moves to the next one. So in your case after having the below code

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

Now the code is already at OrderID, so don't try to read it again. Instead do something like the code below:

        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);
            }
        }

Also make sure that your xml and your model are a match.

cvraman
  • 1,687
  • 1
  • 12
  • 18
0

The customer id is actually being read correctly, it's the line below that which is raising an exception. Your error is on this line of code:

reader.ReadToFollowing("Instructions");

The reason why is that the xml does not contain the element Instructions. You could improve your code by not discarding the result of ReadToFollowing

if (reader.ReadToFollowing("Instructions"))
    reader.ReadElementContentAsString();
Jim
  • 14,952
  • 15
  • 80
  • 167
  • I changed it to the correct element name, but it still points to the same place – Kei Jul 01 '16 at 05:58