【发布时间】:2016-10-03 06:51:46
【问题描述】:
我必须创建一个 xml 文档并通过 LINQ to XML 概念执行以下项目。
- 添加新资产,
- 编辑资产,
- 移除资产。
我正在通过记事本创建一个 xml 文档并通过“asset.xml”保存该文件,然后将该文件复制到调试文件夹中
我的xml文档格式是
<?xml version="1.0" encoding="utf-8"?>
<Assets>
<assetId>1
<assetName>lenova</assetName>
<model>12kj320</model>
<price>22000</price>
<quantity>12</quantity>
</assetId>
</Assets>
我的代码是
class program
{
public static void AddSingleAsset()
{
static List<Asset> Assets = new List<Asset>();
int assetId;
string assetName;
string model;
double price;
int quantity;
assetId = Assets.Count + 1;
Console.WriteLine("Asset id :{0}", assetId);
Console.WriteLine("Enter the asset name");
assetName = Console.ReadLine();
Console.WriteLine("Enter the asset model");
model = Console.ReadLine();
Console.WriteLine("Enter the price of asset");
price = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the quantity of asset");
quantity = int.Parse(Console.ReadLine());
Assets.Add(new Asset() { assetId = assetId, assetName = assetName, modelNo = model, price = price, quantity = quantity });
string path = "linqtoxml1.xml";
XDocument doc = XDocument.Load(path);
doc.Elements("Assets").First().AddFirst(new XElement("assetId", assetId, new XElement("assetName", assetName), new XElement("model", model), new XElement("price", price), new XElement("quantity", quantity)));
doc.Save(path);
}
public static void EditAssetInformation()
{
Console.WriteLine("select the option to edit");
Console.WriteLine("1.To change Asset Name\n2.To change Model NO\n3.To change price\n4.To change Quantity");
int option = int.Parse(Console.ReadLine());
switch (option)
{
case 1:
string newname;
Console.WriteLine("Enter the ID number to change the information");
int id = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Asset new name");
newname = Console.ReadLine();
ModifyName(newname, id);
break;
case 2:
string newModelNo;
Console.WriteLine("Enter the ID number to change the information");
int id1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Asset new model No");
newModelNo = Console.ReadLine();
ModifyModelNo(newModelNo, id1);
break;
case 3:
double newPrice;
Console.WriteLine("Enter the ID number to change the information");
int id2 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Asset new price");
newPrice = int.Parse(Console.ReadLine());
ModifyPrice(newPrice, id2);
break;
case 4:
int newQuantity;
Console.WriteLine("Enter the ID number to change the information");
int id3 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Asset new price");
newQuantity = int.Parse(Console.ReadLine());
Modifyquantity(newQuantity, id3);
break;
}
}
static void ModifyName(string newname, int id)
{
var doc = XElement.Load(path);
var namechange = doc.Element("Assets").Elements("assetId").Where(c => c.Element("assetId").Value == Convert.ToString(id)).Single();
namechange.Element("assetName").Value = newname;
doc.Save(path);
}
static void ModifyModelNo(string newModelNo, int id1)
{
string path = "linqtoxml1.xml";
XDocument doc = XDocument.Load(path);
XElement modelchange = doc.Descendants("AssetId").Where(c => c.Attribute("AssetId").Value.Equals(id1.ToString())).FirstOrDefault();
modelchange.Element("model").Value = newModelNo;
doc.Save(path);
}
static void ModifyPrice(double newPrice, int id2)
{
string path = "linqtoxml1.xml";
XDocument doc = XDocument.Load(path);
XElement pricechange = doc.Descendants("AssetId").Where(c => c.Attribute("AssetId").Value.Equals(id2.ToString())).FirstOrDefault();
pricechange.Element("price").Value = newPrice.ToString();
doc.Save(path);
}
static void Modifyquantity(int newQuantity, int id3)
{
string path = "linqtoxml1.xml";
XDocument doc = XDocument.Load(path);
XElement quantitychange = doc.Descendants("AssetId").Where(c => c.Attribute("AssetId").Value.Equals(id3.ToString())).FirstOrDefault();
quantitychange.Element("quantity").Value = newQuantity.ToString();
doc.Save(path);
}
public static void DeleteAssetInformation()
{
string path = "linqtoxml1.xml";
Console.WriteLine("Enter the ID to delete information");
int id = int.Parse(Console.ReadLine());
XDocument doc = XDocument.Load(path);
XElement cStudent = doc.Descendants("AssetId").Where(c => c.Attribute("ID").Value.Equals(id.ToString())).FirstOrDefault();
cStudent.Remove();
doc.Save(path);
}
}
public class Asset
{
public int assetId
{ get; set; }
public string assetName
{ get; set; }
public string modelNo
{ get; set; }
public double price
{ get; set; }
public int quantity
{ get; set; }
}
我的问题是
我需要自动生成资产 ID。我的代码在我关闭此控制台并再次运行时连续添加元素时有效,然后资产 ID 将从 1 重新开始。
当我尝试编辑或删除上一条记录时,也意味着将显示错误:
SYSTEM.NULL 引用异常
谁能解释一下我在这个程序中做错了什么,并建议我纠正这个问题的简单方法。基本上我来自非 IT 领域,我是一名电气工程师,所以请稍微深入解释一下。
【问题讨论】:
-
From : Where(c => c.Attribute("ID").Value.Equals(id.ToString()) To : Where(c => (int)c == id)
-
你最好使用
XmlSerializer来做这个。完成后对Asset列表/对象进行更改并序列化。您的 LINQ to XML 代码错综复杂(XML 区分大小写)并且混淆了元素和属性。