Kei
Kei

Reputation: 315

C# FormatException was unhandled while processing XML via DOM approach

I've just started learning SAX/Stream as well as DOM approaches for parsing and processing XML files. My scenario would be that I am processing information from a given XML file that is retrieved from an FTP server which I hosted, put it into separate Array-lists before passing the lists into my DBManager to add them into a database. But, before I want to add it into the database, I want the user to press the "check button" to make sure the information is indeed correct before "adding to database". But that somehow triggers a FormatException. I do not see any "DateTime" in any of my classes or variables usage which confuses me even further.

EDIT 1: Thanks to Abdo Hussein, I realized the problem stated in the screenshot was that I was using node, not node2. However, the same problem applies to the following line and possibly the one after, too. Although they are still within the same loop as node and not node2.

invoice.ShippingCharges = Convert.ToDecimal(node.ChildNodes[4].InnerText);
invoice.InvoiceTotal = Convert.ToDecimal(node.ChildNodes[5].InnerText);

enter image description here

FormatException was unhandled

This is the pastebin code for this particular class, since it's too long and messy (I know, I'll leave it for later, I need to get past this first) http://pastebin.com/FewCm23W

Below will be the XML that I'm working on:

<?xml version="1.0" encoding="utf-8" ?>
<Invoices>
  <Invoice ID="I001">    
    <InvoiceDate>21/06/2016</InvoiceDate>
    <SellerID>Supp001</SellerID>
    <BuyerID>WCS1810</BuyerID>
    <OrderID>Order001</OrderID>
    <InvoiceItem>
      <Product ID="R001">
        <ProductName>8GB RAM King</ProductName>
        <Description>RAM</Description>
        <Capacity>8GB</Capacity>
        <Quantity>2</Quantity>
        <UnitPrice>100</UnitPrice>
      </Product>
    </InvoiceItem>
    <ShippingCharges>5</ShippingCharges>
    <InvoiceTotal>205</InvoiceTotal>
  </Invoice>
</Invoices>

These are the classes for the different kind of information to be stored:

Invoice.cs

class Invoice
{
    public string InvoiceID { get; set; }
    public string InvoiceDate { get; set; }
    public string OrderID { get; set; }
    public string SellerID { get; set; }
    public decimal ItemsTotalPrice { get; set; }
    public decimal ShippingCharges { get; set; }
    //shippin charges + itemsTotalPrice
    public decimal InvoiceTotal { get; set; }
}

InvoiceItem

class InvoiceItem
{
    public string InvoiceID { get; set; }
    public string ProductID { get; set; }
    public string Description { get; set; }
    public string Capacity { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal TotalPrice { get; set; }
}

Upvotes: 0

Views: 77

Answers (2)

Kei
Kei

Reputation: 315

Aah, I understand now.

Problem 1- node intead of node2 for each of the decimal variables that I'm converting.

foreach (XmlNode node2 in productList)
{
    decimal qty = Convert.ToDecimal(node.ChildNodes[3].InnerText);
    decimal unitpx = Convert.ToDecimal(node.ChildNodes[4].InnerText);
    totalItemPrice += (qty * unitpx);

}

Problem 2- I didn't know that the entire InvoiceItem was considered a childnode. So swapping my [4] and [5] to [5] and [6] worked.

invoice.ShippingCharges = Convert.ToDecimal(node.ChildNodes[5].InnerText);
invoice.InvoiceTotal = Convert.ToDecimal(node.ChildNodes[6].InnerText);

Upvotes: 1

Abdelmneim Hussein
Abdelmneim Hussein

Reputation: 51

DateTime is just a troubleshooting tip, doesn't mean you have a DateTime Format exception.

You are using node variable in your foreach not the node2 , check this out.

Note that :-

1- FormatException :value is not a number in a valid format.

https://msdn.microsoft.com/en-us/library/9k6z9cdw(v=vs.110).aspx

2- Check your CultureInfo as formats differ depends on different "CultureInfo"s

Upvotes: 1

Related Questions