網站首頁 編程語言 正文
一、DataTable轉XML
#region DataTableToXml
/// <summary>
/// 將DataTable對象轉換成XML字符串
/// </summary>
/// <param name="ds">DataSet對象</param>
/// <returns>XML字符串</returns>
public static string DataTableToXml(DataTable dt,string sName)
{
if (dt != null)
{
MemoryStream ms = null;
XmlTextWriter XmlWt = null;
try
{
ms = new MemoryStream();
//根據ms實例化XmlWt
XmlWt = new XmlTextWriter(ms, System.Text.Encoding.Unicode);
//獲取ds中的數據
dt.TableName = Sql.IsEmptyString(sName) ? "dt2xml" : sName;
dt.WriteXml(XmlWt, XmlWriteMode.WriteSchema);
int count = (int)ms.Length;
byte[] temp = new byte[count];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(temp, 0, count);
//返回Unicode編碼的文本
System.Text.UnicodeEncoding ucode = new System.Text.UnicodeEncoding();
string returnValue = ucode.GetString(temp).Trim();
return returnValue;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
//釋放資源
if (XmlWt != null)
{
XmlWt.Close();
ms.Close();
ms.Dispose();
}
}
}
else
{
return "";
}
}
#endregion
二、XML轉Dataset
方法A:
#region Xml To DataSet
public static DataSet XmlToDataSet(string xmlString)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlString);
StringReader stream = null;
XmlTextReader reader = null;
try
{
DataSet xmlDS = new DataSet();
stream = new StringReader(xmldoc.InnerXml);
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
reader.Close();
return xmlDS;
}
catch (System.Exception ex)
{
reader.Close();
throw ex;
}
}
#endregion
方法B:
private static DataSet XMLToDataset()
{
string strDBXMLFile = @"F:\TestDir\XML\DBTEST.XML";
DataSet dsXML = new DataSet();
dsXML.ReadXml(strDBXMLFile);
//某個節點名稱的所有節點內容
DataTable dtOneNote = dsXML.Tables["SMT"];
return dsXML;
}
三、Dataset轉XML
public static string ConvertDataSetToXML(DataSet xmlDS)
{
MemoryStream stream = null;
XmlTextWriter writer = null;
try
{
stream = new MemoryStream();
//從stream裝載到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode);
//用WriteXml方法寫入文件.
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();
}
}
四、Dataset轉XML文件
public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
{
MemoryStream stream = null;
XmlTextWriter writer = null;
try
{
stream = new MemoryStream();
//從stream裝載到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode);
//用WriteXml方法寫入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
//返回Unicode編碼的文本
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();
}
}
原文鏈接:https://www.cnblogs.com/wml-it/p/15155173.html
相關推薦
- 2022-07-17 SQL?Server格式轉換函數Cast、Convert介紹_MsSql
- 2023-10-15 DPC_WATCHDOG_VIOLATION藍屏分析
- 2022-06-10 Asp.Net?Core使用Ocelot結合Consul實現服務注冊和發現_實用技巧
- 2021-12-18 ECommerceCrawlers項目分析(十二)
- 2022-12-10 OpenMP?共享內存的并行編程框架入門詳解_C 語言
- 2022-12-06 Python基礎globlal?nonlocal和閉包函數裝飾器語法糖_python
- 2022-05-15 C++的數據共享與保護你了解嗎_C 語言
- 2022-12-09 Python曲線擬合多項式深入詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支