I am trying to read the data from XML File,but not able to proceed further.
Here is my XML File:
<CollectionOfVehicles>
<Vehicle>
<Motors>
<Motor>
<Screw>100</Screw>
<Nut>200</Nut>
<Bolt>300</Bolt>
</Motor>
<Motor>
<Screw>100</Screw>
<Nut>200</Nut>
<Bolt>45</Bolt>
</Motor>
</Motors>
<GearBoxes>
<GearBox>
<gear>0</gear>
<Shaft>1</Shaft>
<Bearings>2</Bearings>
</GearBox>
</GearBoxes>
</Vehicle>
</CollectionOfVehicles>
My Data Structure is as follows:
public class Motor
{
public double Screw {get; set; }
public double Nut {get; set; }
public double Bolt {get; set; }
public Motor() {}
}
public class GearBox
{
public uint gear {get; set; }
public uint Shaft {get; set; }
public uint Bearings {get; set; }
public GearBox() {}
}
public class Vehicle
{
public List<Motor> motors {get; set;}
public List<GearBox> gearBoxes {get; set;}
public Vehicle() {}
};
**I tried to write the following lines of code to feed the XML Data:**
Vehicle cars = null;
string path = @"C:\Users\Semantics\Desktop\New.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Vehicle));
var reader = XmlReader.Create(path);
cars = (Vehicle)serializer.Deserialize(reader);
reader.Close();
I basically,want to retrieve Vehicles as and then loop over each vehicle,and retrieve the associated Motor and GearBox.I am not sure,how to retrieve the data like nut,screw and bolt using XMLAttributor (or) XMLElement in my data structure.
Thanks in Advance!
If you are using XML a nice cleaner way to create your objects would be to copy the xml text, then in Visual Studio go to edit->paste special->Paste XML as Classes. That way if your structure changes, you can repeat the process to update your object classes. taking your current xml, and taking in consideration that there could be more than 1 Vehicle, Motor and Gearbox based on your xml structure, this is what I get, Object Models:
[Serializable]
public class CollectionOfVehicles
{
private Vehicle[] vehicleList;
[XmlElementAttribute("Vehicle")]
public Vehicle[] Vehicle { get { return this.vehicleList; } set { this.vehicleList = value; } }
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class Vehicle
{
private Motor[] motorList;
private GearBox[] gearBoxList;
[XmlArray("Motors")]
public Motor[] Motors { get { return this.motorList; } set { this.motorList = value; } }
[XmlArray("GearBoxes")]
public GearBox[] GearBoxes { get { return this.gearBoxList; } set { this.gearBoxList = value; } }
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class Motor
{
private double screwField;
private double nutField;
private double boltField;
public double Screw { get { return this.screwField; } set { this.screwField = value; } }
public double Nut { get { return this.nutField; } set { this.nutField = value; } }
public double Bolt { get { return this.boltField; } set { this.boltField = value; } }
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class GearBox
{
private uint gearField;
private uint shaftField;
private uint bearingsField;
public uint gear { get { return this.gearField; } set { this.gearField = value; } }
public uint Shaft { get { return this.shaftField; } set { this.shaftField = value; } }
public uint Bearings { get { return this.bearingsField; } set { this.bearingsField = value; } }
}
then when reading the xml file you can do the following:
CollectionOfVehicles cars = null;
string path = @"test.xml";
XmlSerializer serializer = new XmlSerializer(typeof(CollectionOfVehicles));
using (var reader = XmlReader.Create(path))
{
cars = (CollectionOfVehicles)serializer.Deserialize(reader);
Console.WriteLine("Vehicles List Count: " + cars.Vehicle.Length.ToString());
Console.WriteLine(" ");
for (int i = 0; i < cars.Vehicle.Length; i++)
{
Console.WriteLine("Vehicle '" + i + "' Motors List Count: " + cars.Vehicle[i].Motors.Length.ToString());
for (int ii = 0; ii < cars.Vehicle[i].Motors.Length; ii++)
{
Console.WriteLine("Vehicle '" + i + "' Motor '" + ii + "' Data");
Console.WriteLine("Bolts: " + cars.Vehicle[i].Motors[ii].Bolt.ToString());
Console.WriteLine("Nut: " + cars.Vehicle[i].Motors[ii].Nut.ToString());
Console.WriteLine("Screws: " + cars.Vehicle[i].Motors[ii].Screw.ToString());
}
Console.WriteLine("Vehicle '" + i + "' GearBoxes List Count: " + cars.Vehicle[i].GearBoxes.Length.ToString());
for (int ii = 0; ii < cars.Vehicle[i].GearBoxes.Length; ii++)
{
Console.WriteLine("Vehicle '" + i + "' GearBox '" + ii + "' Data");
Console.WriteLine("Gear: " + cars.Vehicle[i].GearBoxes[ii].gear.ToString());
Console.WriteLine("Shaft: " + cars.Vehicle[i].GearBoxes[ii].Shaft.ToString());
Console.WriteLine("Bearings: " + cars.Vehicle[i].GearBoxes[ii].Bearings.ToString());
}
}
}