I have to create an xml document and do following item by LINQ to XML concepts.
- add new asset,
- edit asset,
- remove asset.
I was creating a xml document by the notepad and save that file by "asset.xml" then I copy that file into debug folder
my xml document format is
<?xml version="1.0" encoding="utf-8"?>
<Assets>
<assetId>1
<assetName>lenova</assetName>
<model>12kj320</model>
<price>22000</price>
<quantity>12</quantity>
</assetId>
</Assets>
my code is
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; }
}
my problem is
I need to auto generate the asset id. my code is works when I add an element continuously when I close this console and run again then the asset id will start again from 1.
also when I try to EDIT OR DELETE the previous record means that will show the error:
SYSTEM.NULL REFERENCE EXCEPTION
can any one please explain me what I did wrong in this program and also suggest me easy way to rectify this problem. Basically I am from non IT field, I am an ELECTRICAL ENGINEER so explain me in little depth.