您好,登錄后才能下訂單哦!
本篇內容介紹了“C#怎么實現(xiàn)XML文件與DataTable、Dataset互轉”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
#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(); //根據(jù)ms實例化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
方法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); //某個節(jié)點名稱的所有節(jié)點內容 DataTable dtOneNote = dsXML.Tables["SMT"]; return dsXML; }
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(); } }
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(); } }
“C#怎么實現(xiàn)XML文件與DataTable、Dataset互轉”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。