using System;
using System.Collections.Generic;
using System.Text;
namespace xml_operator
{
public class UserAndPass
{
private string fileName;
private string rootName;
private string userNode;
private string userValue;
private string passNode;
private string passValue;
#region method UserAndPass
/// <summary>
/// xml文档构造
/// </summary>
/// <param name="FileName"></param>
/// <param name="RootName"></param>
/// <param name="UserNode"></param>
/// <param name="UserValue"></param>
/// <param name="PassNode"></param>
/// <param name="PassValue"></param>
public UserAndPass(string FileName, string RootName, string UserNode, string UserValue, string PassNode, string PassValue)
{
this.fileName = FileName;
this.rootName = RootName;
this.userNode = UserNode;
this.userValue = UserValue;
this.passNode = PassNode;
this.passValue = PassNode;
}
#endregion
#region method CreateXml
/// <summary>
/// 创建xml文档
/// </summary>
public void CreateXml()
{
XmlTextWriter textWriter = new XmlTextWriter(System.Environment.CurrentDirectory + "//" + this.fileName, null);
textWriter.Formatting = Formatting.Indented;
// 开始写过程,调用WriteStartDocument方法
textWriter.WriteStartDocument();
// 写入说明
textWriter.WriteComment("use the xml for the username and password");
//创建一个节点
textWriter.WriteStartElement(this.rootName);
textWriter.WriteElementString(this.userNode, this.userValue);
textWriter.WriteElementString(this.passNode, this.passValue);
textWriter.WriteEndElement();
// 写文档结束,调用WriteEndDocument方法
textWriter.WriteEndDocument();
// 关闭textWriter
textWriter.Close();
}
#endregion
#region method UpdateXml
/// <summary>
/// 更新xml文档节点关联值
/// </summary>
public void UpdateXml()
{
XmlDocument xmlDoc = new XmlDocument();//修改添加的一行
xmlDoc.Load(System.Environment.CurrentDirectory + "//" + this.fileName);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(this.rootName).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xNode in nodeList)//遍历所有子节点
{
XmlElement xElement = (XmlElement)xNode;//将子节点类型转换为XmlElement类型
if (xElement.Name == this.userNode)
{
xElement.InnerText = this.userValue;
}
if (xElement.Name == this.passNode)
{
xElement.InnerText = this.passValue;
}
}
xmlDoc.Save(System.Environment.CurrentDirectory + "//"+this.fileName);//保存修改
}
#endregion
#region method GetNameValue
/// <summary>
/// 获得Name节点的关联值
/// </summary>
/// <returns></returns>
public string GetNameValue()
{
XmlDocument xmlDoc = new XmlDocument();//修改添加的一行
xmlDoc.Load(System.Environment.CurrentDirectory + "//" + this.fileName);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(this.rootName).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xNode in nodeList)//遍历所有子节点
{
XmlElement xElement = (XmlElement)xNode;//将子节点类型转换为XmlElement类型
if (xElement.Name == this.userNode)
{
return xElement.InnerText;
}
}
}
#endregion
#region method GetPassValue
/// <summary>
/// 获得Pass节点的关联值
/// </summary>
/// <returns></returns>
public stirng GetPassValue()
{
XmlDocument xmlDoc = new XmlDocument();//修改添加的一行
xmlDoc.Load(System.Environment.CurrentDirectory + "//" + this.fileName);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(this.rootName).ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xNode in nodeList)//遍历所有子节点
{
XmlElement xElement = (XmlElement)xNode;//将子节点类型转换为XmlElement类型
if (xElement.Name == this.passNode)
{
return xElement.InnerText;
}
}
}
#endregion
}
}