XML and DataSet conversion classes

Source: Internet
Author: User


Xmldatasetconvert This class provides four methods:
1. Converting XML object content strings to datasets
2. Convert an XML file to a dataset
3. Convert a DataSet to an XML object string
4. Convert a DataSet to an XML file

XmlDatasetConvert.cs using System;
 Using System.Collections.Generic;
 Using System.Text;
 Using System.Data;
 Using System.IO;

Using System.Xml;  Namespace Xmldesign {class Xmldatasetconvert {//Convert XML object content string to dataset public static dataset
             Convertxmltodataset (String xmlData) {StringReader stream = null;
             XmlTextReader reader = null;
                 try {DataSet xmlds = new DataSet ();
                 stream = new StringReader (xmlData);
                 Load from stream to XmlTextReader reader = new XmlTextReader (stream);
                 Xmlds.readxml (reader);
             return xmlds;
             catch (System.Exception ex) {throw ex; finally {if (reader!= null) reader.
             Close (); Convert the XML file to the dataset public static dataset CONVERTXMLFIletodataset (String xmlFile) {StringReader stream = null;
             XmlTextReader reader = null;
                 try {xmldocument xmld = new XmlDocument (); Xmld.

                Load (XmlFile);
                 DataSet Xmlds = new DataSet (); stream = new StringReader (XMLD.
                 INNERXML);
                 Load from stream to XmlTextReader reader = new XmlTextReader (stream);
                 Xmlds.readxml (reader);
                 Xmlds.readxml (XmlFile);
             return xmlds;
             catch (System.Exception ex) {throw ex; finally {if (reader!= null) reader.
             Close ();
             Convert a DataSet to an XML object string public static string Convertdatasettoxml (DataSet Xmlds) {
             MemoryStream stream = null;

            XmlTextWriter writer = null;
  Try           {stream = new MemoryStream ();

                Load from stream to XmlTextReader writer = new XmlTextWriter (stream, Encoding.unicode);
                 Writes a file using the WriteXml method.
                 Xmlds.writexml (writer); int count = (int) stream.
                 Length;
                 byte[] arr = new Byte[count]; Stream.
                 Seek (0, seekorigin.begin); Stream.

                Read (arr, 0, Count);
                 UnicodeEncoding UTF = new unicodeencoding (); Return UTF. GetString (arr).
             Trim ();
             catch (System.Exception ex) {throw ex; finally {if (writer!= null) writer.
             Close ();
         Convert a dataset to an XML file public static void Convertdatasettoxmlfile (DataSet xmlds,string XmlFile)
             {MemoryStream stream = null;

            XmlTextWriter writer = null; Try {stream = new MemoryStream ();

                Load from stream to XmlTextReader writer = new XmlTextWriter (stream, Encoding.unicode);
                 Writes a file using the WriteXml method.
                 Xmlds.writexml (writer); int count = (int) stream.
                 Length;
                 byte[] arr = new Byte[count]; Stream.
                 Seek (0, seekorigin.begin); Stream.

                Read (arr, 0, Count);
                 Returns the Unicode encoded text unicodeencoding UTF = new unicodeencoding ();
                 StreamWriter sw = new StreamWriter (xmlFile); Sw.
                 WriteLine ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>"); Sw. WriteLine (UTF. GetString (arr).
                 Trim ()); Sw.
             Close ();
             catch (System.Exception ex) {throw ex; finally {if (writer!= null) writer.
             Close ();
        } }

    }
 }
 

Using the sample

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Xml;
Using System.Data;

Namespace Xmldesign
{
Class Program
{
static void Main (string[] args)
{
DataSet ds = new DataSet ();

Convert an XML file (local \ Network can) transform an XML file for a dataset#region (local \ Network can) be a dataset
Http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss
F:\study\001CSharp_Study\002Source\XmlDesign\XmlDesign\Save_Plan.xml
ds = Xmldatasetconvert.convertxmlfiletodataset (@ "http://www.bjcan.com/hengxing/" http://news.baidu.com/n?cmd=1 &class=sportnews&tn=rss%22 ");
Console.WriteLine (dataset named \ {0}\) contains {1} tables, ds. DatasetName, DS. Tables.count);
foreach (DataTable dt in DS. Tables)
{
Printtablename (dt. TableName);
};
#endregion

            constructs a dataset and converts it to an XML string #region constructs a dataset, and convert to XML string
            DataSet ds1 = new DataSet ();
            datatable dt1 = new DataTable ();
            dt1. TableName = "Test";
            dt1. Columns.Add ("id");
            dt1. Columns.Add ("name");
            dt1. Rows.Add ("i001", "Hekui");
            dt1. Rows.Add ("i002", "Liyang");

            datatable DT2 = new DataTable ();
            DT2. TableName = "Test1";
            DT2. Columns.Add ("BookID");
            DT2. Columns.Add ("BookName");
            DT2. Rows.Add ("b001", "Book 1");
            DT2. Rows.Add ("b002", "Book 2");

            DS1. Tables.add (DT1);
            DS1. Tables.add (DT2);
            DS1. DatasetName = "scheme";
            String xmlout = Xmldatasetconvert.convertdatasettoxml (DS1);
            #endregion

            Convert an XML string to a dataset#region Converts an XML string to a DataSet
            DataSet ds2 = new DataSet ();
            DS2 = Xmldatasetconvert.convertxmltodataset (xmlout);
            Console.WriteLine (dataset named \ {0}\) contains {1} tables , DS2. DatasetName, DS2. Tables.count);
            foreach (DataTable dt in DS2.) Tables)
            {
                 printtablename (dt. TableName);
           };
            #endregion

Convert a dataset to an XML file #region convert a dataset to an XML file
Xmldatasetconvert.convertdatasettoxmlfile (DS2, "c:\\adadsda1.xml");
#endregion

Console.ReadLine ();
}

private static void Printtablename (String tablename)
{
Console.WriteLine (tablename);
}
}
}



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.