Serialize and deserialize a able using writexml and readxml

Source: Internet
Author: User

Tag: Serialization and deserialization

This article describes how to use writexml and readxml to serialize and deserialize a able. "able does not support schema inference from XML. "Error.

Because datatable implements the iserializable and ixmlserializable interfaces, it supports serialization and deserialization.

Datatable is a good data type.

1. The tables in the Database correspond completely.

2. repleater, DataGrid, and other controls support binding to it.

3. Loop traversal in the Code is particularly convenient.

However, for network transmission or offline storage, the deserialization needs to be serialized into XML. writexml, and readxml is used to achieve the next goal.

1. Use writexml to serialize the able:

Prepare data:

  1. Create Table sitemaps
  2. (
  3. URL varchar (255 ),
  4. Title varchar (255)
  5. )
  6. Insert sitemaps
  7. Select 'HTTP: // www.it118.org ', 'it think tank net'
  8. Union all
  9. Select 'HTTP: // bbs.it118.org ', 'it think tank network BBS'

C # Processing code:

  1. Sqlconnection connection = new sqlconnection ("Server = localhost; database = testdb; uid = sa; Pwd = sa ");
  2. Connection. open ();
  3. Sqlcommand selectcommand = new sqlcommand (string. Format ("select * From sitemaps"), connection );
  4. Datatable = new datatable ("sitemaps"); // note that table must have tablename
  5. New sqldataadapter (selectcommand). Fill (datatable );
  6. Connection. Close ();
  7. Datatable. writexml ("D: \ sitemaps. xml ");

Sitemaps. xml:

  1. <? XML version = "1.0" standalone = "yes"?>
  2. <Documentelement>
  3. <Sitemaps>
  4. <URL> http://www.it118.org </URL>
  5. <Title> it Think Tank Network </title>
  6. </Sitemaps>
  7. <Sitemaps>
  8. <URL> http://bbs.it118.org </URL>
  9. <Title> it Think Tank Network Forum </title>
  10. </Sitemaps>
  11. </Documentelement>
Ii. Use readxml to serialize the able:

If the code is as follows:

  1. Datatable dt = new datatable ();
  2. DT. readxml ("D: \ sitemaps. xml ");

The following message is displayed:Datatable does not support schema inference from XML.

The correct code is as follows:

  1. Datatable dt = new datatable ("sitemaps ");
  2. DT. Columns. Add ("url ");
  3. DT. Columns. Add ("title ");
  4. DT. readxml ("D: \ sitemaps. xml ");
3. Application

I used to have special requirements. I used writexml to generate xml locally and transmit it to other places to read it using readxml.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.