資料表序列化使用的類檔案代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace WindowsFormsApp1
{
class DataToXml
{
public static bool CDataToXmlFile(DataTable dt, string xmlFilePath)
{
if ((dt != null) && (!string.IsNullOrEmpty(xmlFilePath)))
{
string path = Application.StartupPath;
MemoryStream ms = null;
XmlTextWriter XmlWt = null;
try
{
ms = new MemoryStream();
//根據ms實體化XmlWt
XmlWt = new XmlTextWriter(ms, Encoding.Unicode);
//獲取ds中的資料
dt.WriteXml(XmlWt);
int count = (int)ms.Length;
byte[] temp = new byte[count];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(temp, 0, count);
//回傳Unicode編碼的文本
UnicodeEncoding ucode = new UnicodeEncoding();
//寫檔案
StreamWriter sw = new StreamWriter(path);
sw.WriteLine("<?xml version="1.0" encoding="utf - 8"?>");
sw.WriteLine(ucode.GetString(temp).Trim());
sw.Close();
return true;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
//釋放資源
if (XmlWt != null)
{
XmlWt.Close();
ms.Close();
ms.Dispose();
}
}
}
else
{
return false;
}
}
}
}
呼叫代碼:
string xmlFilePath = wenjian.xml;
DataToXml.CDataToXmlFile(dt, xmlFilePath);
編譯后出現例外資訊,顯示下列陳述句有錯誤:
sw.WriteLine("<?xml version="1.0" encoding="utf - 8"?>");
嚴重性 代碼 說明 專案 檔案 行 禁止顯示狀態
錯誤 CS1003 語法錯誤,應輸入“,” WindowsFormsApp1 D:\資料表操作\WindowsFormsApp1\WindowsFormsApp1\DataToXml.cs 38 活動的
嚴重性 代碼 說明 專案 檔案 行 禁止顯示狀態
錯誤 CS0246 未能找到型別或命名空間名“DataTable”(是否缺少 using 指令或程式集參考?) WindowsFormsApp1 D:\資料表操作\WindowsFormsApp1\WindowsFormsApp1\DataToXml.cs 15 活動的
百度了一下,CS1003可能是asp.net程式錯誤,WindowsFormsApp不能用asp.net程式?
CS0246可能是由于使用資料表而缺少參考了什么。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/259897.html
標籤:C#
上一篇:core 呼叫另一個上傳專用的控制器里的方法,老提示NullReferenceException之類的錯誤,不知什么原因
下一篇:VS2019資料庫遷移時出現問題Format of the initialization string does not conform to specific
