Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First I create DataSet object
C#
DataSet ds = new DataSet();

Then I load xmlscheme.
C#
ds.ReadXmlSchema("C:/Users/Levan/Dcuments/Visual Studio 2012/Projects/Crashop/CarLibrary/CarDataSet.xsd");
It load's fine.I use Dataset visualizer for debugging.Xmlscheme has following structure Table
Cars(carname(string),carnumber(string),cardiscription(string),CarID(int)).Than I try loadxml
C#
ds.ReadXml("C:/Users/Levan/Documents/Visual Studio 2012/Projects/Crashop/CarLibrary/CarPark.xml");
xml file has following structure
XML
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Cars>
    <carname>BMW</carname>
    <carnumber>AA2935IO</carnumber>
    <cardiscription>Nice car</cardiscription>
    <CarID>1</CarID>
  </Cars>
  <Cars>
    <carname>Mersedes</carname>
    <carnumber>AA2235IO</carnumber>
    <cardiscription>Nice car</cardiscription>
    <CarID>2</CarID>
  </Cars>
</NewDataSet>

In result no errors,but empty table.
C#
ds.Tables["Cars"].Rows.Add("BMW","AA2935IO","Nice Car",1);
Works fine.But i want to figure out how to read from XML file.In example which i use everything good and i didn't find appropriate solution for me.
Posted
Updated 20-Sep-15 10:32am
v3
Comments
George Swan 20-Sep-15 16:26pm    
In your XML schema you have
'canumber(string)' - shouldn't that be carnumber NOT canumber
Inimicos 20-Sep-15 16:31pm    
сarnumber there i wrote here wrong.

1 solution

There is - at least - few ways to read/write xml data, for example:
1. Linq to XML[^]
2. Xml Serialization/deserialization[^]

Based on this[^] i'd suggest to create custom class collection as follow:

Car class is used to store data about single car.
C#
public class Car
{
    private string sCarName = string.Empty;
    private string sCarNumber = string.Empty;
    private string sCarDescription = string.Empty;
    private int iCarId = 0;

    //contructor
    public Car(string _CarName, string _CarNumber, string _CarDescription, int _CarId)
    {
        sCarName = _CarName;
        sCarNumber = _CarNumber;
        sCarDescription = _CarDescription;
        iCarId = _CarId;
    }

    public string CarName
    {
        get{return sCarName;}
        set{sCarName = value;}
    }

    public string CarNumber
    {
        get{return sCarNumber;}
        set{sCarNumber = value;}
    }

    public string CarDescription
    {
        get{return sCarDescription;}
        set{sCarDescription = value;}
    }

    public int CarID
    {
        get{return iCarId;}
        set{iCarId = value;}
    }

}


Cars class inherits from CollectionBase and is used to store list of cars (of type: Car):
C#
public class Cars : CollectionBase
{

    public Car Item(int index)
    {
	if(index<0 || index>List.Count-1)
		throw new IndexOutOfRangeException();
		
	return (Car)List[index];
    }

    public Car Add(Car _car)
    {
        _car.CarID = List.Count + 1;
        int index = List.Add(_car);
        return (Car)List[index];
    }

    void Remove(int index)
    {
        if(index<0 || index>List.Count-1)
            throw new IndexOutOfRangeException();

        List.Remove(index);
    }

}


Usage:

C#
XDocument xdoc = XDocument.Load(@"D:\CarPark.xml");
Cars CarPark = new Cars(); //collection of cars

    //fetch data from xml document
    //create List<Car>
var cars = xdoc.Descendants("Car")
        .Select(c=> new Car
        (
            (string)c.Element("CarName"),
            (string)c.Element("CarNumber"),
            (string)c.Element("CarDescription"),
            (int)c.Element("CarID")
        ));
//fill collection: add cars from List<Car>
foreach(Car c in cars)
{
    CarPark.Add(c);
}

    //CarPark is ready to use, you can access single car using Item method.
Car cr = CarPark.Item(0);
cr.CarName = "Seat Toledo";


Note: you have to make some minor changes in xml file before you'll be able to use it:
XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Cars>
  <Car>
    <CarName>BMW</CarName>
    <CarNumber>AA2935IO</CarNumber>
    <CarDescription>Nice car</CarDescription>
    <CarID>1</CarID>
  </Car>
  <Car>
    <CarName>Mersedes</CarName>
    <CarNumber>AA2235IO</CarNumber>
    <CarDescription>Nice car</CarDescription>
    <CarID>2</CarID>
  </Car>
</Cars>


If you want to implement Save method, which will dump all CarPark data into xml file, you have to loop through the collection of Cars:
C#
XDocument xdoc = new XDocument();
XElement root = new XElement("Cars");
foreach(Car c in CarPark)
{
    root.Add(new XElement("Car",
                    new XElement("CarName", c.CarName),
                    new XElement("CarNumber", c.CarNumber),
                    new XElement("CarDescription", c.CarDescription),
                    new XElement("CarID", c.CarID)));
}
xdoc.Add(root);
xdoc.Save(@"FullPathToXmlFile.xml");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900