XML and DataSet conversion classes

Source: Internet
Author: User
Tags foreach object contains copy count return string trim
xml| Conversion

To give you a class that converts XML to dataset:

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
Copy C # source code using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Data;
Using System.IO;
Using System.Xml;

Namespace Xmldesign
{
Class Xmldatasetconvert
{
Converts an XML object content string to a 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 an XML file to a 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 ();
}
}

Converts 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 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
Copy C # source code 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 ();
#region Convert an XML file (local \ Network can) to 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://news.baidu.com/n?cmd=1&class=sportnews&tn=rss");
Console.WriteLine (dataset named \ {0}\) contains {1} tables, ds. DatasetName, DS. Tables.count);
foreach (DataTable dt in DS. Tables)
{
Printtablename (dt. TableName);
}
#endregion

The

            #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");

The

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

            #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

#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.