【问题标题】:How to count below extensions? by using c#如何计算以下扩展名?通过使用 c#
【发布时间】: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


【解决方案1】:

提示:“未找到文件”应该告诉您在哪里为您的代码提供文件名...但是既然您在提出问题时遇到了所有麻烦,请尝试以下操作:

1) 检查您的路径变量 - 将其更改为包含文件的整个路径 (@"c:\MyDirectory\Assets.xml")

2) 改变

 XDocument doc = XDocument.Load("path");

 XDocument doc = XDocument.Load(path);

看看会发生什么。如果仍有错误,请报告。

【讨论】:

  • 你好我修改了你说的,我修改的是,string path = (@"C:\Users\arunkumar.nagarajan\Documents\Visual Studio 2015\Projects\Employe Assets (linq to xmll)\使用资产 (linq to xmll)\bin\Debug.Assets.xml"); XDocument doc = XDocument.Load(path);这将在字符串路径 = (@"C:\Users\arunkumar.nagarajan\Documents\Visual Studio 2015\Projects\Employe Assets (linq to xmll)\Employe Assets (linq to xmll)\bin\Debug.资产.xml"); XDocument doc = XDocument.Load(path);
  • 如果我像下面这样修改这意味着 string path = ("Assets.xml"); XDocument doc = XDocument.Load(path);我得到 System.Xml.dll 中发生“System.Xml.XmlException”类型的未处理异常
  • 这是一条不同的消息,因此我们修复了“找不到文件”。错误信息和来源是什么?
猜你喜欢
  • 1970-01-01
  • 2018-04-29
  • 1970-01-01
  • 2012-11-12
  • 2019-01-19
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
相关资源
最近更新 更多