XmlDatasetConvert類提供的四種方法

2022-09-15 19:54:10 字數 3045 閱讀 4512

xmldatasetconvert 該類提供了四種方法:

1、將xml物件內容字串轉換為dataset

2、將xml檔案轉換為dataset

3、將dataset轉換為xml物件字串

4、將dataset轉換為xml檔案

xmldatasetconvert.cs

using system;

using system.collections.generic;

using system.text;

using system.data;

using system.io;

using system.xml;

namespace xmldesign

class xmldatasetconvert

//將xml物件內容字串轉換為dataset

public static dataset convertxmltodataset(string xmldata)

stringreader stream = null;

xmltextreader reader = null;

trydataset xmlds = new dataset();

stream = new stringreader(xmldata);

//從stream裝載到xmltextreader

reader = new xmltextreader(stream);

xmlds.readxml(reader);

return xmlds;

catch (system.exception ex)

throw ex;

finally

if (reader != null) reader.close();

//將xml檔案轉換為dataset

public static dataset convertxmlfiletodataset(string xmlfile)

stringreader stream = null;

xmltextreader reader = null;

tryxmldocument xmld = new xmldocument();

xmld.load(xmlfile);

dataset xmlds = new dataset();

stream = new stringreader(xmld.innerxml);

//從stream裝載到xmltextreader

reader = new xmltextreader(stream);

xmlds.readxml(reader);

return xmlds;

catch (system.exception ex)

throw ex;

finally

if (reader != null) reader.close();

//將dataset轉換為xml物件字串

public static string convertdatasettoxml(dataset xmlds)

memorystream stream = null;

xmltextwriter writer = null;

trystream = 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;

trystream = 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();

標準容器類提供的四種迭代器型別

四種型別分別為 iterator const iterator reverse iterator const reverse iterator 對於container而言,iterator的作用相當於t 而const iterator則相當於const t 你可能也見過t const 這樣的寫法 增...

HTTP請求Content Type四種方式比較

1 content type作用 乙個正確的http請求應當具備狀態行 請求頭 訊息主體 請求的資料應當放到訊息主體裡,資料本身的編碼方式由content type宣告,請求的客戶端和服務端必須遵守同乙個編碼方式才能正確解析資料的內容 常用於原生的 form 表單提交 3 multipart for...

UML類的四種關係

1.例項化 依賴 a將b作為區域性變數進行使用.程式1void a foo 2.關聯 a與b存在一定的關係.這裡只考慮單向導航.關聯在 中有多種表現形式.第一種,作為引數 程式2void a foo b b b b or b b 第二種,作為成員變數 程式3class a void foo priv...