What's the easiest way to read/write xml?

A

Andy B

I have this good sized xsd file I created (219 lines long). It is supposed
to be a representation of a contract a business uses. I need to know what
the best and easiest way is to create and use xml files with this schema. I
will be doing a lot of adding/changing/editing of the xml files too. I tried
to look at XmlDocument but got totally lost. Any ideas on how to overcome
this problem?
 
A

Alberto Poblacion

Andy B said:
I have this good sized xsd file I created (219 lines long). It is supposed
to be a representation of a contract a business uses. I need to know what
the best and easiest way is to create and use xml files with this schema. I
will be doing a lot of adding/changing/editing of the xml files too. I
tried to look at XmlDocument but got totally lost. Any ideas on how to
overcome this problem?

The easiest way is probably the following:
- Use XSD.EXE to generate a class from your xsd file.
- Add the class to your project. Create an instance and fill it with
data.
- Use the XmlSerializer to serialize your class into a file. The file
will be xml and conform to the schema that you originally described in the
xsd.
 
A

Andy B

How do you use the XmlSerializer?


Alberto Poblacion said:
The easiest way is probably the following:
- Use XSD.EXE to generate a class from your xsd file.
- Add the class to your project. Create an instance and fill it with
data.
- Use the XmlSerializer to serialize your class into a file. The file
will be xml and conform to the schema that you originally described in the
xsd.
 
A

Alberto Poblacion

Andy B said:
How do you use the XmlSerializer?

Like this:

using System.Xml.Serialization;
using System.IO;
...

TheClass myInstance = new TheClass();
myInstance.someField = theValue;
...

XmlSerializer serializer = new XmlSerializer(typeof(TheClass));
TextWriter writer = new StreamWriter(@"C:\TEMP\TheFile.xml");
serializer.Serialize(writer, myInstance);
writer.Close();
 
F

Fabricio

Like this:

using System.Xml.Serialization;
using System.IO;
...

TheClass myInstance = new TheClass();
myInstance.someField = theValue;
...

XmlSerializer serializer = new XmlSerializer(typeof(TheClass));
TextWriter writer = new StreamWriter(@"C:\TEMP\TheFile.xml");
serializer.Serialize(writer, myInstance);
writer.Close();

You can have another option:

Use this XSD file as a Dataset file, if possible.
Then you can work with XML files using Dataset methods (WriteXML and
ReadXML, i guess).
So, the XML file works as a table and its rows.
 

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