我刚刚开始学习SAX/Stream以及解析和处理XML文件的DOM方法。我的情况是我正在处理从我托管的FTP服务器检索的给定XML文件中的信息,并在将这些列表传递到我的DBManager中以将它们添加到数据库中之前将其放入单独的数组列表中。但是,在我想将它添加到数据库之前,我希望用户在“添加到数据库”之前按下“检查按钮”以确保信息确实正确。但是不知何故触发了一个FormatException。我没有看到我的任何类或变量使用中的任何“DateTime”,这让我更加困惑。通过DOM处理XML时,C#FormatException未处理
编辑1:感谢阿卜杜·侯赛因,我意识到在截图中所述的问题是,我使用节点,不节点2。然而,同样的问题也适用于下面的行,也可能是后面的行。尽管它们仍处于节点而不是node2的相同循环内。
invoice.ShippingCharges = Convert.ToDecimal(node.ChildNodes[4].InnerText);
invoice.InvoiceTotal = Convert.ToDecimal(node.ChildNodes[5].InnerText);
这是该特定类的引擎收录的代码,因为它太长而凌乱(我知道,我会离开它之后,我需要让过去这首)http://pastebin.com/FewCm23W
下面将是我工作的XML:
<?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>
这些都是存储在不同种类的信息类:
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; }
}
小心指定哪个foreach? – Kei
因为我想能够计算节点2中的值并将其存储在节点 – Kei
Aaah中,在休息并研究其他内容并返回之后,我看到了node2愚蠢的错误。但问题是,对于这些行: invoice.ShippingCharges = Convert.ToDecimal(node.ChildNodes [4] .InnerText); invoice.InvoiceTotal = Convert.ToDecimal(node.ChildNodes [5] .InnerText); 问题仍然存在 – Kei