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:
- Create Table sitemaps
- (
- URL varchar (255 ),
- Title varchar (255)
- )
- Insert sitemaps
- Select 'HTTP: // www.it118.org ', 'it think tank net'
- Union all
- Select 'HTTP: // bbs.it118.org ', 'it think tank network BBS'
C # Processing code:
- Sqlconnection connection = new sqlconnection ("Server = localhost; database = testdb; uid = sa; Pwd = sa ");
- Connection. open ();
- Sqlcommand selectcommand = new sqlcommand (string. Format ("select * From sitemaps"), connection );
- Datatable = new datatable ("sitemaps"); // note that table must have tablename
- New sqldataadapter (selectcommand). Fill (datatable );
- Connection. Close ();
- Datatable. writexml ("D: \ sitemaps. xml ");
Sitemaps. xml:
- <? XML version = "1.0" standalone = "yes"?>
- <Documentelement>
- <Sitemaps>
- <URL> http://www.it118.org </URL>
- <Title> it Think Tank Network </title>
- </Sitemaps>
- <Sitemaps>
- <URL> http://bbs.it118.org </URL>
- <Title> it Think Tank Network Forum </title>
- </Sitemaps>
- </Documentelement>
Ii. Use readxml to serialize the able:
If the code is as follows:
- Datatable dt = new datatable ();
- DT. readxml ("D: \ sitemaps. xml ");
The following message is displayed:Datatable does not support schema inference from XML.
The correct code is as follows:
- Datatable dt = new datatable ("sitemaps ");
- DT. Columns. Add ("url ");
- DT. Columns. Add ("title ");
- 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.