zoukankan
html css js c++ java
一个用于读写XML文档的类
//
writer:furenjun 2006.05.05
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
using
System.Xml;
using
System.Xml.Xsl;
using
System.Xml.XPath;
using
System.Text;
namespace
MrfuReadAndWriteXmlFile
{
/**/
///
<summary>
///
C_MrFuXmlManager 的摘要说明。
///
</summary>
public
class
C_MrFuXmlManager
{
public
C_MrFuXmlManager()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
string
myXmlFilePath
=
Application.StartupPath
+
@"
\Users.xml
"
;
string
tempFilePath
=
Application.StartupPath
+
@"
\Temp.xml
"
;
public
string
GsXmlFilePath
{
get
{
return
myXmlFilePath;}
set
{myXmlFilePath
=
value;}
}
XmlDocument Operation
#region
XmlDocument Operation
/**/
///
<summary>
///
创建xml文档
///
</summary>
///
<returns></returns>
public
bool
CreateMyXmlFile()
{
try
{
XmlTextWriter myTextWriter
=
new
XmlTextWriter (
this
.GsXmlFilePath ,System.Text.Encoding.GetEncoding(
"
GB2312
"
) );
myTextWriter.Formatting
=
Formatting.Indented;
//
写入文本的缩进格式
myTextWriter.WriteStartDocument(
true
);
//
设置standalone="no"
//
当为(true)时 <?xml version="1.0" standalone="yes" ?>
//
如果 XML 文档所需要的所有实体声明都包含在文档内,则有效值为 yes,或者如果需要外部 DTD,则为 no。如果 XML 声明中没有独立特性,该属性将返回 String.Empty。
myTextWriter.WriteDocType(
"
mrfuDocType
"
,
null
,
null
,
null
) ;
myTextWriter.WriteComment(
"
System user info,warning : do not modify!
"
);
//
添加注释
//
myTextWriter.WriteStartElement("mrfu");
//
Write the namespace declaration.
//
myTextWriter.WriteAttributeString("xmlns", "Users", null, "urn:UsersInfo");
//
为前面的element写属性
myTextWriter.WriteStartElement(
"
mrfu
"
,
"
Users
"
,
"
urn:UsersInfo
"
);
//
myTextWriter.WriteElementString("Name","Fu") ;
//
myTextWriter.WriteElementString("Pwd","renjun") ;
//
myTextWriter.WriteElementString("DateOfLastlogin","06/08/1982") ;
myTextWriter.WriteEndElement() ;
//
myTextWriter.WriteEndElement() ;
//
与上呼应,对应一个单元 区间.
//
write the xml to file and close the myTextWriter
myTextWriter.Flush();
myTextWriter.Close();
//
MessageBox.Show ("写入数据完毕.");
}
catch
{
return
false
;
}
return
true
;
}
/**/
///
<summary>
///
返回指定xml文档的数据集
///
</summary>
///
<returns></returns>
public
DataSet GetMyXmlData()
{
DataSet myDataSet
=
null
;
try
{
myDataSet
=
new
DataSet();
myDataSet.ReadXml(
this
.GsXmlFilePath );
//
MessageBox.Show (myDataSet.Tables[0].TableName.ToString () );
}
catch
(Exception e1)
{
MessageBox.Show (e1.Message.ToString() ) ;
}
return
myDataSet;
}
/**/
///
<summary>
///
插入数据至xml文档
///
</summary>
///
<param name="usInfo"></param>
///
<returns></returns>
public
bool
InsertDataInXmlFile(UserInfo usInfo)
{
try
{
XmlTextWriter TWriter
=
new
XmlTextWriter(
this
.tempFilePath ,Encoding.GetEncoding(
"
GB2312
"
) );
TWriter.WriteStartDocument();
TWriter.WriteStartElement(
"
mrfu
"
,
"
Users
"
,
"
urn:UsersInfo
"
);
TWriter.WriteStartElement(
"
User
"
);
TWriter.WriteAttributeString(
"
Id
"
,usInfo.Id);
//
对前面的User写出具有指定的本地名称和值的属性
TWriter.WriteElementString(
"
Name
"
,usInfo.Name );
TWriter.WriteElementString(
"
Pwd
"
,usInfo.Pwd );
TWriter.WriteElementString(
"
AreaFlag
"
,usInfo.AreaFlag );
TWriter.WriteElementString(
"
DateOfLastlogin
"
,System.DateTime.Now.ToString() );
TWriter.WriteEndElement();
TWriter.WriteEndElement();
TWriter.WriteEndDocument();
TWriter.Flush();
TWriter.Close();
SaveUserInfo(
this
.tempFilePath );
}
catch
{
return
false
;
}
return
true
;
}
/**/
///
<summary>
///
保存数据至xml文档,与前面的插入方法结合使用.
///
</summary>
///
<param name="path"></param>
private
void
SaveUserInfo(
string
path)
{
XmlDocument xdoc
=
new
XmlDocument();
xdoc.Load(
this
.GsXmlFilePath );
XmlDocument xdocImp
=
new
XmlDocument();
xdocImp.Load(path);
XmlNodeList pNodes
=
xdocImp.SelectNodes(
"
//User
"
);
NameTable nametable
=
new
NameTable();
XmlNamespaceManager nameMan
=
new
XmlNamespaceManager(nametable);
nameMan.AddNamespace(
"
mrfu
"
,
"
urn:UsersInfo
"
);
foreach
(XmlNode pNode
in
pNodes)
{
XmlNode pNodeImp
=
xdoc.SelectSingleNode(
"
//User[Name='
"
+
pNode.ChildNodes.Item(
0
).InnerXml
+
"
']
"
);
if
(pNodeImp
==
null
)
{
XmlNode importedNode
=
xdoc.ImportNode(pNode,
true
);
xdoc.SelectSingleNode(
"
//mrfu:Users
"
,nameMan).AppendChild(importedNode);
}
}
xdoc.Save(
this
.GsXmlFilePath );
}
#endregion
/**/
///
<summary>
///
按用户名查找用户的相关信息
///
</summary>
///
<param name="UserName"></param>
///
<returns></returns>
public
UserInfo GetUserInfo(
string
UserName)
{
UserInfo usInfo
=
new
UserInfo();
if
(
this
.GetNumOfChild(UserName)
<
0
)
return
usInfo;
XmlDocument xdoc
=
new
XmlDocument();
xdoc.Load(
this
.GsXmlFilePath );
XmlNodeList xList
=
xdoc.SelectNodes(
"
//User[Name='
"
+
UserName
+
"
']
"
);
foreach
(XmlNode node
in
xList)
{
XmlNodeReader NReader
=
new
XmlNodeReader(node);
try
{
while
(NReader.Read())
{
if
(NReader.NodeType
==
XmlNodeType.Element)
{
if
(NReader.HasAttributes)
{
while
(NReader.MoveToNextAttribute())
{
if
(NReader.Value
!=
"
urn:UsersInfo
"
)
{
usInfo.Id
=
NReader.Value;
}
}
}
}
if
(NReader.Name
==
"
Name
"
)
{
usInfo.Name
=
NReader.ReadString();
}
else
if
(NReader.Name
==
"
Pwd
"
)
{
usInfo.Pwd
=
NReader.ReadString();
}
else
if
(NReader.Name
==
"
AreaFlag
"
)
{
usInfo.AreaFlag
=
NReader.ReadString();
}
else
if
(NReader.Name
==
"
DateOfLastlogin
"
)
{
usInfo.DateOfLastlogin
=
NReader.ReadString();
}
}
}
catch
(Exception err)
{
string
errmsg
=
"
read xml file error \n
"
+
err.ToString();
MessageBox.Show(errmsg);
}
finally
{
if
(NReader
!=
null
)
{
NReader.Close();
}
}
}
return
usInfo;
}
/**/
///
<summary>
///
查找用户信息返回用户id的数据表
///
</summary>
///
<param name="UserName"></param>
///
<returns></returns>
public
DataTable GetUserID(
string
UserName)
{
DataTable dt
=
new
DataTable();
if
(
this
.GetNumOfChild(UserName)
<
0
)
return
dt;
XmlDocument xdoc
=
new
XmlDocument();
xdoc.Load(
this
.GsXmlFilePath );
XmlNodeList xList
=
xdoc.SelectNodes(
"
//User[Name='
"
+
UserName
+
"
']
"
);
//
可以根据实际需要更改它的查找范围.如SelectNodes("
//
User")
if
(xList.Count
>
0
)
{
CreateColumns(dt, xList[
0
]);
}
foreach
(XmlNode node
in
xList)
{
DataRow dr
=
dt.NewRow();
foreach
(XmlAttribute attr
in
node.Attributes)
{
dr[attr.Name]
=
attr.Value;
}
dt.Rows.Add(dr);
}
return
dt;
}
/**/
///
<summary>
///
创建列
///
</summary>
///
<param name="dt"></param>
///
<param name="node"></param>
protected
static
void
CreateColumns(DataTable dt, XmlNode node)
{
foreach
(XmlAttribute attr
in
node.Attributes)
{
dt.Columns.Add(
new
DataColumn(attr.Name));
}
}
/**/
///
<summary>
///
查找当前用户结点的位置
///
</summary>
///
<param name="UserName"></param>
///
<returns></returns>
public
int
GetNumOfChild(
string
UserName)
{
if
(
!
VerifyParameters())
return
-
1
;
int
i
=-
1
;
ArrayList myArylist
=
new
ArrayList();
XmlDocument xdoc
=
new
XmlDocument();
xdoc.Load(
this
.GsXmlFilePath );
XmlNodeList xList
=
xdoc.SelectNodes(
"
//User
"
);
int
j
=
0
;
int
totalNum
=
xList.Count ;
bool
find
=
false
;
while
((j
<
totalNum)
&&
(
!
find))
{
if
(xList[j].ChildNodes.Item(
0
).InnerText.Trim()
==
UserName.Trim() )
{
i
=
j;
find
=
true
;
}
j
++
;
}
return
i;
}
/**/
///
<summary>
///
获取所有的用户名称
///
</summary>
///
<returns></returns>
public
ArrayList GetTheNameOfAllUser( )
{
ArrayList myArylist
=
new
ArrayList();
XmlDocument xdoc
=
new
XmlDocument();
xdoc.Load(
this
.GsXmlFilePath );
XmlNodeList xList
=
xdoc.SelectNodes(
"
//User
"
);
foreach
(XmlNode xNode
in
xList)
{
myArylist.Add(xNode.ChildNodes.Item(
0
).InnerText);
//
也可显示文档中的其它项,更改item(i)即可
}
return
myArylist;
}
/**/
///
<summary>
///
更新指定用户的信息
///
</summary>
///
<param name="UserName"></param>
///
<param name="usInfo"></param>
///
<returns></returns>
public
bool
UpdateNotes(
string
UserName,UserInfo usInfo)
{
if
(
this
.GetNumOfChild(UserName)
<
0
)
return
false
;
try
{
XmlDocument xdoc
=
new
XmlDocument();
xdoc.Load(
this
.GsXmlFilePath );
//
创建一个文档片断来存储要替换的节点
XmlDocumentFragment xdocFrag
=
xdoc.CreateDocumentFragment();
//
新节点的根节点
XmlElement newElement
=
xdoc.CreateElement(
"
User
"
);
XmlAttribute newID
=
xdoc.CreateAttribute(
"
Id
"
);
newID.Value
=
usInfo.Id ;
newElement.Attributes.SetNamedItem(newID);
XmlElement newName
=
xdoc.CreateElement(
"
Name
"
);
newName.InnerText
=
UserName;
newElement.AppendChild(newName);
XmlElement newPwd
=
xdoc.CreateElement(
"
Pwd
"
);
newPwd.InnerText
=
usInfo.Pwd ;
newElement.AppendChild(newPwd);
XmlElement newAreaFlag
=
xdoc.CreateElement(
"
AreaFlag
"
);
newAreaFlag.InnerText
=
usInfo.AreaFlag ;
newElement.AppendChild(newAreaFlag);
XmlElement newDateOfLastlogin
=
xdoc.CreateElement(
"
DateOfLastlogin
"
);
newDateOfLastlogin.InnerText
=
System.DateTime.Now.ToString() ;
newElement.AppendChild(newDateOfLastlogin);
//
把新创建的节点加入文档片断
xdocFrag.AppendChild(newElement);
//
确定要替换的节点作替换
NameTable nametable
=
new
NameTable();
XmlNamespaceManager nameMan
=
new
XmlNamespaceManager(nametable);
nameMan.AddNamespace(
"
mrfu
"
,
"
urn:UsersInfo
"
);
XmlElement xUsers
=
(XmlElement)xdoc.SelectSingleNode(
"
//mrfu:Users
"
,nameMan);
//
找到该用户在文档中的结点序号(第几个孩子)
int
i
=
GetNumOfChild(UserName);
if
(i
>=
0
)
{
XmlElement xChild
=
(XmlElement)xUsers.ChildNodes[i];
xUsers.ReplaceChild(xdocFrag.FirstChild,xChild);
xdoc.Save(
this
.GsXmlFilePath );
}
else
return
false
;
}
catch
{
return
false
;
}
return
true
;
}
/**/
///
<summary>
///
删除指定用户的纪录
///
</summary>
///
<param name="UserName"></param>
///
<returns></returns>
public
bool
DeleteNotes(
string
UserName)
{
try
{
XmlDocument xdoc
=
new
XmlDocument();
xdoc.Load(
this
.GsXmlFilePath );
NameTable nametable
=
new
NameTable();
XmlNamespaceManager nameMan
=
new
XmlNamespaceManager(nametable);
nameMan.AddNamespace(
"
mrfu
"
,
"
urn:UsersInfo
"
);
XmlNode rootnode
=
xdoc.SelectSingleNode(
"
//mrfu:Users
"
,nameMan);
XmlNode node
=
xdoc.SelectSingleNode(
"
//User[Name='
"
+
UserName
+
"
']
"
);
rootnode.RemoveChild(node);
xdoc.Save(
this
.GsXmlFilePath );
}
catch
{
return
false
;
}
return
true
;
}
/**/
///
<summary>
///
验证xml文档的有效性
///
</summary>
///
<returns></returns>
protected
bool
VerifyParameters()
{
bool
bls
=
true
;
if
(
!
System.IO.File.Exists (
this
.GsXmlFilePath ))
{
bls
=
false
;
throw
(
new
Exception(
"
xmlFile do not exists.
"
));
}
else
{
XmlDocument doc
=
new
XmlDocument();
doc.Load(
this
.GsXmlFilePath );
if
(doc
==
null
)
{
bls
=
false
;
throw
new
Exception(
"
doc cannot be null.
"
);
}
if
(doc.LastChild.GetType()
==
typeof
(System.Xml.XmlDeclaration))
{
bls
=
false
;
throw
new
Exception(
"
XmlDocument requires at least the a root node
"
);
}
}
return
bls;
}
/**/
///
<summary>
///
获取当前根结点下的所有纪录值
///
</summary>
///
<returns></returns>
public
DataTable GetDataFromXmlFile( )
{
VerifyParameters();
XmlDocument doc
=
new
XmlDocument();
doc.Load(
this
.GsXmlFilePath );
DataTable myTable
=
new
DataTable();
XmlNodeList xList
=
doc.SelectNodes(
"
//User
"
);
//
create data table
DataTable dt
=
new
DataTable();
//
读取它的元素名称
dt.Columns.Add(
new
DataColumn(
"
Id
"
));
dt.Columns.Add(
new
DataColumn(
"
Pwd
"
));
dt.Columns.Add(
new
DataColumn(
"
AreaFlag
"
));
dt.Columns.Add(
new
DataColumn(
"
DateOfLastlogin
"
));
//
将各个元素对应的值一行一行写入表中
DataRow row
=
null
;
foreach
(XmlNode xNode
in
xList)
{
row
=
dt.NewRow();
for
(
int
j
=
0
;j
<
4
;j
++
)
{
row[j]
=
xNode.ChildNodes.Item(j).InnerText;
}
dt.Rows.Add( row );
}
dt.AcceptChanges();
return
dt;
}
}
public
class
UserInfo
{
public
string
Id
=
""
;
public
String Name
=
""
;
public
string
Pwd
=
""
;
public
string
AreaFlag
=
""
;
public
string
DateOfLastlogin
=
""
;
//
public String Symbol="";
//
public double Last;
//
public DateTime Date;
//
public double Change;
//
public double Open;
//
public double High;
//
public double Low;
//
public long Volume;
//
public long MarketCap;
//
public double PreviousClose;
//
public double PreviousChange;
//
public double Low52Week;
//
public double High52Week;
}
}
查看全文
相关阅读:
小学生学python(六)类与函数
Windows 10 搭建 Flask
CentOS 8 上安装 python3
6_7 selenium使用代理IP
6_6 模拟浏览器的前进后退&窗口句柄切换
6_5 selenium操作cookie
6_4 行为链
6_3 selenium操作表单元素
6_2 selenium定位元素的方法
6_1 selenium 安装与 chromedriver安装
原文地址:https://www.cnblogs.com/furenjun/p/401567.html
最新文章
python命令行使用参数
Mac小技巧
MAC安装FSL步骤以及问题详解
生成器和生成器表达式
Python基础(三)
C# HtmlAgilityPack
C# NPOI
C# WCF
C# WinForm
Python Xml
热门文章
FFmpeg
c# 计时器
C# Lock 关键字
C# 动态编译
神州数码 SF服务
gcc4.8.5下strncpy的坑
stl中的一点常规用法
程序是如何跑起来的
小学生学python(七)练习
快速排序算法(c++)
Copyright © 2011-2022 走看看