Read XML file using OOP

T

Tammy Nejadian

Hi,
I am using visual studio C# window and I have an xml file which I need to
read that file using Object Oriented program. What codes I should use in my
class to load and read that xml file. Thanks

My xml file call carsXml.xml and it includes below information:
<?xml version="1.0" encoding="utf-8" ?>
<carlot>
<car>
<Make>OldsMobile</Make>
<Model>Cutlas</Model>
<Milage>5000</Milage>
</car>
<car>
<Make>Chevelate</Make>
<Model>Camro</Model>
<Milage>6000</Milage>
</car>
</carlot>
My Class calls carClass however it is not completed. I dont know how to lead
that xml file here to read it. The codes I have are:
class carClass
{
//Field
private string _model;
private string _make;
private int _milage;

//properties
public string Model
{
get { return _model; }
set { _model = value; }
}

public string Make
{
get { return _make; }
set {_make = value; }
}

public int Milage
{
get { return _milage; }
set { _milage = value;}
}
}
 
M

Martin Honnen

Tammy said:
My xml file call carsXml.xml and it includes below information:
<?xml version="1.0" encoding="utf-8" ?>
<carlot>
<car>
<Make>OldsMobile</Make>
<Model>Cutlas</Model>
<Milage>5000</Milage>
</car>
<car>
<Make>Chevelate</Make>
<Model>Camro</Model>
<Milage>6000</Milage>
</car>
</carlot>
My Class calls carClass however it is not completed. I dont know how to lead
that xml file here to read it. The codes I have are:
class carClass
{
//Field
private string _model;
private string _make;
private int _milage;

//properties
public string Model
{
get { return _model; }
set { _model = value; }
}

public string Make
{
get { return _make; }
set {_make = value; }
}

public int Milage
{
get { return _milage; }
set { _milage = value;}
}
}

You could use XML serialization like this:

[XmlRoot(ElementName="carlot")]
public class Carlot
{
public Carlot() { }
private carClass[] cars;

[XmlElement(ElementName = "car")]
public carClass[] Cars
{
get { return cars; }
set { cars = value; }
}
}

public class carClass
{
public carClass() { }
//Field
private string _model;
private string _make;
private int _milage;

//properties
public string Model
{
get { return _model; }
set { _model = value; }
}

public string Make
{
get { return _make; }
set { _make = value; }
}

public int Milage
{
get { return _milage; }
set { _milage = value; }
}
}

then you can load your XML like this:

XmlSerializer ser = new XmlSerializer(typeof(Carlot));
Carlot carlot =
(Carlot)ser.Deserialize(XmlReader.Create(@"..\..\XMLFile1.xml"));
foreach (carClass car in carlot.Cars)
{
Console.WriteLine("Make: {0}; Model: {1}; Milage:
{2}.", car.Make, car.Model, car.Milage);
}
 
D

Dick Swager

using namespace System::Xml; // XmlTextReader,
XmlValidatingReader

....

void XmlStuff (String^ xmlFilename)
{
try
{
// Open the XML file that has the XML text.
XmlDocument^ doc = gcnew XmlDocument ();
doc->Load (xmlFilename);

XmlNode^ carNode = doc->SelectSingleNode ("/carlot/car");

if (!carNode
return;

// Get the make of the car.
string make = gcnew String carNode->Attributes
["Make"]->Value);

...
}

catch (Exception^ e)
{
throw gcnew Exception ("Could not obtain info from the XML
file: " + xmlFilename, e);
}
}
 
M

Marc Gravell

Here's a trick... put the xml in a file, load the VS command prompt
(from the start menu), and type;

xsd myfile.xml
xsd myfile.xsd /classes

The first line generates an xsd from your sample xml; the second line
generates C# from the xsd. You can use this either directly, or to see
how the Xml attributes work. To load/save, use XmlSerializer - i.e.

XmlSerializer ser = new XmlSerializer(typeof(CarLot));

CarLot lot = new CarLot
{
Cars = {
new Car { Make = "A", Milage=322, Model ="avc"},
new Car { Make = "A", Milage=322, Model ="avc"}
}
};
StringWriter writer = new StringWriter();
ser.Serialize(writer, lot);
string xml = writer.ToString();
Trace.WriteLine(writer);
XmlReader reader = XmlReader.Create(new StringReader(xml));
lot = (CarLot) ser.Deserialize(reader);

Marc
 
T

Tammy Nejadian

Thanks all for your help. Now were I should use these codes? in class or in
Form? I used that in the form the place I want to display the information
from xml and I get error message for this part:
xsd myfile.xml
xsd myfile.xsd /classes
 
M

Marc Gravell

get error message for this part:

Perhaps you misunderstood me. "xsd" is a command line utility that can
be used during development to translate between xml, xsd and C#. It
isn't something that goes into your actual code - rather it is one of
the ways of *getting* the code, or at least starting off (so you don't
need to code your entire object model by hand and then try and figure
out which property attributes to use).

Marc
 
M

Martin Honnen

Tammy said:
Thanks all for your help. Now were I should use these codes? in class or in
Form? I used that in the form the place I want to display the information
from xml and I get error message for this part:
xsd myfile.xml
xsd myfile.xsd /classes

xsd.exe is a command line tool which is part of the .NET SDK. So you
need to use it from the (.NET framework) "SDK command prompt" or the
"Visual Studio command prompt".

The documentation is here:
http://msdn2.microsoft.com/en-us/library/x6c1kb0s(VS.80).aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top