会员
周边
众包
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
dotnetbbs
博客园
首页
新随笔
联系
订阅
管理
讀取XML文檔的Demo
讀取/修改/刪除 XML文檔的Demo
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Xml;
namespace
XMLApp
{
class
clsXML
{
private
static
string
_fileName;
private
static
int
id;
private
static
string
name;
private
static
string
title;
private
static
string
author;
private
static
XmlDocument xmlDoc
=
null
;
private
static
XmlNamespaceManager nsmgr
=
null
;
private
static
XmlNode rootNode
=
null
;
public
static
int
ID
{
get
{
return
id; }
set
{ id
=
value; }
}
public
static
string
Name
{
get
{
return
name; }
set
{ name
=
value; }
}
public
static
string
Title
{
get
{
return
title; }
set
{ title
=
value; }
}
public
static
string
Author
{
get
{
return
author; }
set
{ author
=
value; }
}
public
static
void
OpenXmlFile(
string
fileName)
{
_fileName
=
fileName;
if
(xmlDoc
==
null
)
{
xmlDoc
=
new
XmlDocument();
xmlDoc.Load(fileName);
//
nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
//
nsmgr.AddNamespace("myXml", "
http://www.publishing.com
");
rootNode
=
xmlDoc.DocumentElement;
//
SelectSingleNode("
//
myXml:book", nsmgr);
}
}
public
static
void
LocateXmlRecord(
int
id)
{
if
(xmlDoc
!=
null
)
{
XmlNode node
=
xmlDoc.SelectSingleNode(
"
Company/Department/Books/book[@id='
"
+
id.ToString()
+
"
']
"
);
if
(node
!=
null
)
{
//
id = (int)node["id"].InnerText;
name
=
node[
"
Name
"
].InnerText;
title
=
node[
"
Title
"
].InnerText;
author
=
node[
"
Author
"
].InnerText;
}
}
}
public
static
void
ModifyXmlRecord(
int
id)
{
if
(xmlDoc
!=
null
)
{
XmlNode node
=
xmlDoc.SelectSingleNode(
"
Company/Department/Books/book[@id='
"
+
id.ToString()
+
"
']
"
);
if
(node
!=
null
)
{
node[
"
Name
"
].InnerText
=
"
testName
"
;
node[
"
Title
"
].InnerText
=
"
testTitle
"
;
node[
"
Author
"
].InnerText
=
"
testAuthor
"
;
xmlDoc.Save(_fileName);
}
}
}
public
static
void
DeleteXmlRecord(
int
id)
{
if
(xmlDoc
!=
null
)
{
XmlNode fNode
=
xmlDoc.SelectSingleNode(
"
Company/Department/Books
"
);
XmlNode node
=
xmlDoc.SelectSingleNode(
"
Company/Department/Books/book[@id='
"
+
id.ToString()
+
"
']
"
);
if
(node
!=
null
)
{
fNode.RemoveChild(node);
}
xmlDoc.Save(_fileName);
}
}
//
public static void DeleteXmlRecord(int id)
//
{
//
if (xmlDoc != null)
//
{
//
XmlNode fNode = xmlDoc.SelectSingleNode("Company/Department/Books");
//
XmlNode node = xmlDoc.SelectSingleNode("Company/Department/Books/book[@id='" + id.ToString() + "']");
//
if (node != null)
//
{
//
fNode.RemoveChild(node);
//
}
//
xmlDoc.Save(_fileName);
//
}
//
}
}
}
XML文件如下:
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<
Company
>
<
Department id
=
"
101
"
>
<
Department_Name
>
Cai WuBu
</
Department_Name
>
<
Manager
>
Zhang Bin
</
Manager
>
<
Books
>
<
book id
=
"
1
"
>
<
Name
>
testName1
</
Name
>
<
Title
>
testTitle1
</
Title
>
<
Author
>
testAuthor1
</
Author
>
</
book
>
<
book id
=
"
2
"
>
<
Name
>
testName
</
Name
>
<
Title
>
testTitle
</
Title
>
<
Author
>
testAuthor
</
Author
>
</
book
>
<
book id
=
"
3
"
>
<
Name
>
book3
</
Name
>
<
Title
>
333333
</
Title
>
<
Author
>
DinoFung
</
Author
>
</
book
>
</
Books
>
</
Department
>
</
Company
>
发表于
2006-05-11 13:17
RogerWang
阅读(
417
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告