【发布时间】:2016-09-28 19:52:42
【问题描述】:
我需要通过下面的 C# 代码添加一个新的资产细节!
当我尝试编译代码时,我得到:“FILE NOT FOUND EXCEPTION WAS UNHANDLED”。
如何修改程序? - 这个异常的原因是什么?
我的 Xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<Assets>
<Asset>
<assetId></assetId>
<assetName></assetName>
<modelNo></modelNo>
<price></price>
<quantity></quantity>
</Asset>
我的 C# 代码:
static List<Asset> Assets = new List<Asset>();
public static void AddSingleAsset() {
Asset newAsset = new Asset();
newAsset.assetId = Assets.Count + 1;
Console.WriteLine("Asset ID : {0}", newAsset.assetId);
Console.WriteLine("Enter the asset name");
newAsset.assetName = Console.ReadLine();
Console.WriteLine("Model number :");
newAsset.modelNo = Console.ReadLine();
Console.WriteLine("Price :");
newAsset.price = double.Parse(Console.ReadLine());
Console.WriteLine("Quantity :");
newAsset.quantity = int.Parse(Console.ReadLine());
Assets.Add(newAsset);
string path = "Assets.xml";
XDocument doc = XDocument.Load(path);
doc.Elements("Assets").First().Add(new XElement("Asset", new XAttribute("assetId", newAsset.assetId),
new XElement("assetName", newAsset.assetName),
new XElement("modelNo", newAsset.modelNo),
new XElement("price", newAsset.price),
new XElement("quantity", newAsset.quantity)
));
doc.Save(path);
}
【问题讨论】:
-
What is the reason for this exception?我认为很清楚。FILE NOT FOUND
标签: c# linq linq-to-xml