網(wǎng)站首頁 編程語言 正文
一、DataTable轉(zhuǎn)XML
#region DataTableToXml
/// <summary>
/// 將DataTable對(duì)象轉(zhuǎn)換成XML字符串
/// </summary>
/// <param name="ds">DataSet對(duì)象</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();
//根據(jù)ms實(shí)例化XmlWt
XmlWt = new XmlTextWriter(ms, System.Text.Encoding.Unicode);
//獲取ds中的數(shù)據(jù)
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轉(zhuǎn)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);
//某個(gè)節(jié)點(diǎn)名稱的所有節(jié)點(diǎn)內(nèi)容
DataTable dtOneNote = dsXML.Tables["SMT"];
return dsXML;
}
三、Dataset轉(zhuǎn)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轉(zhuǎn)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
相關(guān)推薦
- 2022-04-18 python中出現(xiàn)invalid?syntax報(bào)錯(cuò)的幾種原因分析_python
- 2022-10-08 LyScript實(shí)現(xiàn)繞過反調(diào)試保護(hù)的示例詳解_python
- 2023-03-27 詳解C/C++高精度(加減乘除)算法中的壓位優(yōu)化_C 語言
- 2022-05-18 C語言程序環(huán)境和預(yù)處理詳解分析_C 語言
- 2022-08-19 vscode遠(yuǎn)程免密登入Linux服務(wù)器的配置方法_Linux
- 2022-12-11 C語言冷知識(shí)之預(yù)處理字符串操作符詳解_C 語言
- 2022-05-03 如何判斷?.NET?Core?應(yīng)用程序以管理員身份運(yùn)行的_實(shí)用技巧
- 2023-01-28 Flutter?Widget移動(dòng)UI框架使用Material和密匙Key實(shí)戰(zhàn)_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支