C#: Read/Write XML to/from Database

person Jason Huangfolder_openC#, Codelocal_offer, , access_time September 9, 2009

It’s really easy to write a System.Data.DataTable to an XML file, and you can later Read the XML file back into a DataTable.

First, you need to give the DataTable a name (very important otherwise save table to xml would fail without a tablename), then you want to write the datatable schema to a xml file first. You should then write the data itself into another XML file. So when you’re ready to read the XML file back into a datatable, just load the table schema xml file first, then load the data xml file.

in the following example, I first use traditional ADO.NET to populate some data into table booklist,
then I gave the table name “booklist”, without the tablename, when you writeXML you will get an exception.

Then I use “WriteXmlSchema” function to write the schema of the datatable to a file, then I use WriteXml function to write the actual data to another file.

To Load, I first create a new datatable, then use ReadXmlSchema to read the schema file first to set the data table structure, then read the data by using ReadXml function.


//first populate the DataTable booklist
 
//write the table definition & data into XML file.
string filename_data = @"c:\\data.xml";
string filename_schema = @"c:\\schema.xml";
this.booklist.TableName = "booklist";
this.booklist.WriteXml(filename_data);
this.booklist.WriteXmlSchema(filename_schema);
 
//read the XML files and populate into a new data table
this.booklist = new DataTable();
string filename_data = @"c:\\data.xml";
string filename_schema = @"c:\\schema.xml";
this.booklist.ReadXmlSchema(filename_schema);
this.booklist.ReadXml(filename_data);

Comments

  1. Super post, Need to mark it on Digg

  2. For this reason the media must ensure that when they publicize
    a protest they get the full story before holding
    the authority to ransom. This is super awesome for me since I enjoy doing it 100%.
    Together we create social media strategies, dig out content sources that make sense,
    and organize everything into a marketing calendar with a
    great process defined. Frydrych states that on the first day of school,
    a female student posted from campus that she was sad. She
    was hesitant to let me filter the invitations
    to only people who were in the general area of where she was performing
    because she didn’t want to “drop off the radar”
    of the other fans. Internet marketing has flourished rapidly in past few years due to the optimum increase in online shopping.
    It’s not a good idea to spam people in your news feed, and you can’t get a hundred new
    friends a day, unless you can convince them to look for you and add you.
    Social media marketing is the way to go these days. Although few
    phones support the feature, you can sometimes fake your location information or edit the EXIF data directly as well.
    Do not judge the company based on how many businesses
    they’ve managed for in the past.

    Here is my webpage delray beach social media marketing fees

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>