C 讀寫XML文件

2021-06-23 05:54:01 字數 4531 閱讀 2032



c#讀取xml文件

使用xmldocument物件,可以很方便的讀取xml文件,c#中對xml的操作進行的封裝,相比較於c++,則要方便很多。

下面是樣例xml文件:

[html]view plain

copy

print?

<?

xmlversion="1.0"

encoding="utf-8"

standalone="yes"

?>

<

root

>

<

continents

>

<

continent

id="101"

name="asia"

/>

<

continent

id="102"

name="africa"

/>

<

continent

id="103"

name="north america"

/>

<

continent

id="104"

name="south america"

/>

<

continent

id="105"

name="europe"

/>

<

continent

id="106"

name="oceania"

/>

<

continent

id="107"

name="antarctica"

/>

continents

>

root

>

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

讀取該文件的樣例**如下:

[csharp]view plain

copy

print?

string strfilepathname = strfilepath + "\\continent.xml";  

listdata = new list();  

console.writeline("the data is parsed from file :" + strfilepathname);  

try  

xmlnode continentnode = xmlnode.firstchild;  

foreach (xmlnode node in continentnode.childnodes)  

}  catch (exception ex)  

console.writeline("parsed ok, result:");  

foreach (continent info in data)    

string strfilepathname = strfilepath + "\\continent.xml";

listdata = new list();

console.writeline("the data is parsed from file :" + strfilepathname);

tryxmlnode continentnode = xmlnode.firstchild;

foreach (xmlnode node in continentnode.childnodes)

}catch (exception ex)

console.writeline("parsed ok, result:");

foreach (continent info in data)

c# 寫xml文件

c#寫xml文件也比較簡單,首先使用xmldocument物件建立xml文件,然後寫入xmlwriter流,進行flush就行了。

下面是樣例**:

[csharp]view plain

copy

print?

string strfilepathname = strfilepath + "\\continent.xml";  

// write

// asia, africa, north america, south america,europe, oceania, antarctica

listdata = new list();  

uint uiidbegin = 101;  

data.add(new continent(uiidbegin++, "asia"));  

data.add(new continent(uiidbegin++, "africa"));  

data.add(new continent(uiidbegin++, "north america"));  

data.add(new continent(uiidbegin++, "south america"));  

data.add(new continent(uiidbegin++, "europe"));  

data.add(new continent(uiidbegin++, "oceania"));  

data.add(new continent(uiidbegin++, "antarctica"));  

foreach (continent info in data)  

console.writeline("the data will write to file :" + strfilepathname);  

try  

xmlwritersettings wrsettings = new xmlwritersettings();  

wrsettings.indent = true;  

wrsettings.encoding = unicodeencoding.utf8;  

xmlwriter xmlwr = xmlwriter.create(strfilepathname, wrsettings);  

xmldoc.writeto(xmlwr);  

xmlwr.flush();  

console.writeline("create ok, file name = " + strfilepathname);  

}  catch (exception ex)    

string strfilepathname = strfilepath + "\\continent.xml";

// write

// asia, africa, north america, south america,europe, oceania, antarctica

listdata = new list();

uint uiidbegin = 101;

data.add(new continent(uiidbegin++, "asia"));

data.add(new continent(uiidbegin++, "africa"));

data.add(new continent(uiidbegin++, "north america"));

data.add(new continent(uiidbegin++, "south america"));

data.add(new continent(uiidbegin++, "europe"));

data.add(new continent(uiidbegin++, "oceania"));

data.add(new continent(uiidbegin++, "antarctica"));

foreach (continent info in data)

console.writeline("the data will write to file :" + strfilepathname);

tryxmlwritersettings wrsettings = new xmlwritersettings();

wrsettings.indent = true;

wrsettings.encoding = unicodeencoding.utf8;

xmlwriter xmlwr = xmlwriter.create(strfilepathname, wrsettings);

xmldoc.writeto(xmlwr);

xmlwr.flush();

console.writeline("create ok, file name = " + strfilepathname);

}catch (exception ex)

後記:這裡僅僅是個簡單的例子,以備往後複製黏貼之用,呵呵!

通過XmlDocument讀寫Xml文件

通過xmldocument讀寫xml文件 xmlversion 1.0 encoding utf 8 students studentname 張平 courses coursename 語文?teachercomment 這裡是語文老師的批註 coursename 數學 teachercommen...

Android讀寫XML(下) 建立XML文件

類說明 xmlserializerdefine an inte ce to serialziation of xml infoset.定義乙個介面來實現xml資訊的序列化。物件的串型化,也有叫做物件的序列話,並不只是簡單的把物件儲存在儲存器上,它可以使我們在流中傳輸物件,使物件變的可以像基本資料一樣...

Android讀寫XML(下) 建立XML文件

by 海市蜃樓 in android開發 23九2009 android讀寫xml 中 sax 類說明 xmlserializerdefine an inte ce to serialziation of xml infoset.定義乙個介面來實現xml資訊的序列化。物件的串型化,也有叫做物件的序列...