Home > Net >  About XML with DataTable transform each other
About XML with DataTable transform each other

Time:02-13

From the Internet to find a to convert the DataTable XML code, just a static method, a class file, including the static method, so that the program (the back three using statements were added my own), as follows:


Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;
Using System. IO;
Using System. The Runtime. Serialization;
Using System. The Runtime. Serialization. Formatters. Binary;


The namespace WindowsFormsApp1
{
The class ConvertData
{
Public static string CDataToXml DataTable (dt)
{
If (dt!=null)
{
MemoryStream ms=null;
XmlTextWriter XmlWt=null;
Try
{
Ms=new MemoryStream ();
//according to ms instantiation XmlWt
XmlWt=new XmlTextWriter (ms, Encoding. Unicode);
//retrieve data from the ds
Dt. WriteXml (XmlWt);
Int count=(int) ms. Length;
Byte [] temp=new byte (count);
Ms. Seek (0, SeekOrigin. Begin);
Ms. Read (temp, 0, count);
//returns the Unicode text
UnicodeEncoding ucode=new UnicodeEncoding ();
String returnValue=https://bbs.csdn.net/topics/ucode.GetString (temp). The Trim ();
Return the returnValue.
}
The catch (System. Exception ex)
{
Throw the ex.
}
The finally
{
//release resources
If (XmlWt!=null)
{
XmlWt. Close ();
Ms. Close ();
Ms. The Dispose ();
}
}
}
The else
{
Return "";
}
}
}
}

In the program using method is as follows:
String XmlValue;

XmlValue=https://bbs.csdn.net/topics/ConvertData.CDataToXml (dt);
RichTextBox1. Text=XmlValue;

There have been some mistakes, but compile time specific as follows:


Severity code shows the project file line prohibits display status
Error CS0246 failed to find the type or namespace name "DataTable" (whether the lack of a using directive or assembly reference?) WindowsFormsApp1 D: \ XML \ WindowsFormsApp1 \ WindowsFormsApp1 \ ConvertData cs 15 activities of


Severity code shows the project file line prohibits display status
Error CS0246 failed to find the type or namespace name "XmlTextWriter" (whether the lack of a using directive or assembly reference?) WindowsFormsApp1 D: \ XML \ WindowsFormsApp1 \ WindowsFormsApp1 \ ConvertData cs 20 activity

Severity code shows the project file line prohibits display status
Error CS0246 failed to find the type or namespace name "XmlTextWriter" (whether the lack of a using directive or assembly reference?) WindowsFormsApp1 D: \ XML \ WindowsFormsApp1 \ WindowsFormsApp1 \ ConvertData cs 25 activity

Severity code shows the project file line prohibits display status
Error CS1503 parameter 1: from "System. The Data. The DataTable" into "the DataTable WindowsFormsApp1" D: \ XML \ WindowsFormsApp1 \ WindowsFormsApp1 \ Form1 cs activity of 59

Please comment

CodePudding user response:

Can participate

CodePudding user response:

Can participate
  •  Tags:  
  • C#
  • Related