I have function that creates xml modify it and display in console. I would like to add something like menu to this app and add function that modify it and then depends on this file create collection of objects of new created class and display it on screen. I would like also to add menu to this. For example when user choose 1 then it creates xml file, choose 2 then modify it and create collection of objects and then number 3 to display it on screen.
Creating xml file
using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
namespace MyApp // Note: actual namespace depends on the project name.
{
internal class Program
{
static void Main(string[] args)
{
List<Car> CarsList = new List<Car>();
CarsList.Add(new Car("BMW", "X3", "Red", 2005));
CarsList.Add(new Car("Audi", "A7", "Red", 2012));
CarsList.Add(new Car("Mercedes", "S550", "Red", 2019));
CarsList.Add(new Car("Subaru", "Impreza", "Black", 2002));
CarsList.Add(new Car("Toyota", "Yaris", "Black", 2015));
CarsList.Add(new Car("VW", "Golf", "Black", 2013));
CarsList.Add(new Car("Peugeot", "508", "Black", 2001));
CarsList.Add(new Car("Citroen", "C3", "Black", 2022));
CarsList.Add(new Car("Ford", "Mustang", "Blue", 2013));
CarsList.Add(new Car("Volvo", "V50", "Green", 2011));
XDocument xml = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Cars list"),
new XElement("Cars",
from Car in CarsList
orderby Car.Brand, Car.Model
select new XElement("Auto",
new XAttribute("Brand", Car.Brand),
new XElement("Model", Car.Model),
new XElement("Color", Car.Color),
new XElement("Year", Car.Year)
)
)
);
xml.Save("Cars.xml");
}
}
}
Modify xml file
using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
namespace MyApp // Note: actual namespace depends on the project name.
{
internal class Program
{
static void Main(string[] args)
{
XDocument xml = XDocument.Load("Cars.xml");
var cars = xml.Root.Elements("Car").Where(
Cars => Cars.Attribute("Brand").Value == "Audi");
if (cars.Any())
cars.First().Element("model").Value = "Test";
xml.Save("Cars.xml");
xml.Save(Console.Out);
}
}
}
Function to display it in console
using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
namespace MyApp // Note: actual namespace depends on the project name.
{
internal class Program
{
static void Main(string[] args)
{
XDocument xml = XDocument.Load("Cars.xml");
List<Car> CarsList = (
from Car in xml.Root.Elements("Car")
select new Car(
Car.Attribute("Brand").Value,
Car.Element("Model").Value,
Car.Element("Color").Value,
int.Parse(Car.Element("Year").Value)
)
).ToList();
xml.Save(Console.Out);
}
}
}
class Car
{
public string Brand { get; set; }
public string Model { get; set; }
public string Color { get; set; }
public int Year { get; set; }
public Car(string Brand, string Model, string Color, int Year)
{
Brand = Brand;
Model = Model;
Color = Color;
Year = Year;
}
}
CodePudding user response:
I think it is better to do this project using DataTables where you can read and write to XML with one instruction. Also can display the table in one instruction to a DGV. See example below :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataTable dt = null;
const string FILENAME = @"c:\temp\test.xml";
public Form1()
{
InitializeComponent();
dt = new DataTable("Cars");
dt.Columns.Add("Brand", typeof(string));
dt.Columns.Add("Model", typeof(string));
dt.Columns.Add("Color", typeof(string));
dt.Columns.Add("Year", typeof(int));
dt.Rows.Add(new object[] {"BMW", "X3", "Red", 2005});
dt.Rows.Add(new object[] {"Audi", "A7", "Red", 2012});
dt.Rows.Add(new object[] {"Mercedes", "S550", "Red", 2019});
dt.Rows.Add(new object[] {"Subaru", "Impreza", "Black", 2002});
dt.Rows.Add(new object[] {"Toyota", "Yaris", "Black", 2015});
dt.Rows.Add(new object[] {"VW", "Golf", "Black", 2013});
dt.Rows.Add(new object[] {"Peugeot", "508", "Black", 2001});
dt.Rows.Add(new object[] {"Citroen", "C3", "Black", 2022});
dt.Rows.Add(new object[] {"Ford", "Mustang", "Blue", 2013});
dt.Rows.Add(new object[] {"Volvo", "V50", "Green", 2011});
dataGridView1.DataSource = dt;
dt.WriteXml(FILENAME);
}
}
}