xml文件的如何去读
1,使用XmlDocument 类分析xml文件的节点等信息读取
2,使用DataSet类,将Xml文件Load进DataSet中,当成表来读
3,序列化
1:如一楼所示,完整的如:
/// <summary>
/// 插入评委
/// </summary>
/// <param name="key">评委编号</param>
/// <param name="name">评委名称</param>
/// <returns>评委编号</returns>
public void Update(int key, string name, bool isNb)
{
if (!File.Exists(DataPath))
{
throw new Exception("修改失败,数据文件不存在!");
}
if (!this.Exist(key))
{
throw new Exception("修改失败,编号不存在!");
}
bool isModify = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(DataPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/base").ChildNodes;
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = xn as XmlElement;
if (xe != null && xe.GetAttribute("key") == key.ToString())
{
xe.SetAttribute("name", name);
if (isNb)
{
xe.SetAttribute("nb", "1");
}
else
{
xe.SetAttribute("nb", "0");
}
isModify = true;
}
}
xmlDoc.Save(DataPath);
}
catch
{
throw new Exception("修改失败,出现未知异常!");
}
if (!isModify)
{
throw new Exception("修改失败,节点不存在!请重置XML文件!");
}
}
2,DataSet读取,百度一下,例子找不到了
3,序列化
/// <summary>
/// 加载用户信息表
/// </summary>
/// <returns></returns>
public CtekOsmUser LoadOsmUser()
{
string userInfoPath = ApplicationData.ExePath + ApplicationData.UserInfoFilePath;
if (!File.Exists(userInfoPath))
{
if (!InitManageUser())
{
throw new Exception("初始化用户数据失败!");
}
}
string userXmlStr = GetUserFileStr(userInfoPath);
if (userXmlStr == null)
{
throw new Exception("获取用户数据文件失败!");
}
CtekOsmUser users = SerializeCtekOsmUser(userXmlStr);
if (users == null)
{
throw new Exception("读取用户数据失败!");
}
return users;
}