using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;
namespace RDCJ3Client.Util
{
class LoginUser
{
public LoginUser()
{ }
public static void CreateXml(string User_id)
{
string CreateFilePath = System.IO.Directory.GetCurrentDirectory() + "\\myUserXml.xml";
if (System.IO.File.Exists(CreateFilePath))
{
//遍历xml文件的信息
GetXMLInformation(CreateFilePath, User_id);
}
else
{
//生成xml文件
GenerateXMLFile(CreateFilePath, User_id);
}
//修改xml文件的信息
ModifyXmlInformation(CreateFilePath);
//向xml文件添加节点信息
AddXmlInformation(CreateFilePath);
//删除指定节点信息
DeleteXmlInformation(CreateFilePath);
}
private static void GenerateXMLFile(string xmlFilePath, string User_id)
{
try
{
//初始化一个xml实例
XmlDocument myXmlDoc = new XmlDocument();
XmlDeclaration xmlDeclare = myXmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
//创建xml的根节点
XmlElement rootElement = myXmlDoc.CreateElement("Nodes");
//将根节点加入到xml文件中(AppendChild)
myXmlDoc.AppendChild(rootElement);
//初始化第一层的第一个子节点
XmlElement firstLevelElement1 = myXmlDoc.CreateElement("User");
//填充第一层的第一个子节点的属性值(SetAttribute)
firstLevelElement1.SetAttribute("Name", "" + User_id + "");
//将第一层的第一个子节点加入到根节点下
rootElement.AppendChild(firstLevelElement1);
//初始化第二层的第一个子节点
XmlElement secondLevelElement11 = myXmlDoc.CreateElement("LiginTime");
//填充第二层的第一个子节点的值(InnerText)
secondLevelElement11.InnerText = DateTime.Now.ToString();
firstLevelElement1.AppendChild(secondLevelElement11);
//将xml文件保存到指定的路径下
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void GetXMLInformation(string xmlFilePath, string User_id)
{
bool IsXmlNode = false;
try
{
//初始化一个xml实例
XmlDocument myXmlDoc = new XmlDocument();
//加载xml文件(参数为xml文件的路径)
myXmlDoc.Load(xmlFilePath);
//获得第一个姓名匹配的节点(SelectSingleNode):此xml文件的根节点
XmlNode rootNode = myXmlDoc.SelectSingleNode("Nodes");
//分别获得该节点的InnerXml和OuterXml信息
string innerXmlInfo = rootNode.InnerXml.ToString();
string outerXmlInfo = rootNode.OuterXml.ToString();
//获得该节点的子节点(即:该节点的第一层子节点)
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
//获得该节点的属性集合
XmlAttributeCollection attributeCol = node.Attributes;
//
//{
//获取属性名称与属性值
//string name = attri.Name;
//string value = attri.Value;
if (node.Attributes["Name"].Value.Equals(User_id) && node.HasChildNodes)
{
XmlNode secondLevelNode1 = node.FirstChild;
string name = secondLevelNode1.Name;
string innerText = secondLevelNode1.InnerText;
secondLevelNode1.InnerText = DateTime.Now.ToString();
IsXmlNode = true;
myXmlDoc.Save(xmlFilePath);
}
//}
判断此节点是否还有子节点
//if (node.HasChildNodes)
//{
// //获取该节点的第一个子节点
// XmlNode secondLevelNode1 = node.FirstChild;
// //获取该节点的名字
// string name = secondLevelNode1.Name;
// //获取该节点的值(即:InnerText)
// string innerText = secondLevelNode1.InnerText;
// //获取该节点的第二个子节点(用数组下标获取)
// //XmlNode secondLevelNode2 = node.ChildNodes[1];
// //name = secondLevelNode2.Name;
// //innerText = secondLevelNode2.InnerText;
// //Console.WriteLine("{0} = {1}", name, innerText);
//}
}
if (!IsXmlNode)
{
//初始化第一层的第一个子节点
XmlElement firstLevelElement1 = myXmlDoc.CreateElement("User");
//填充第一层的第一个子节点的属性值(SetAttribute)
firstLevelElement1.SetAttribute("Name", "" + User_id + "");
//将第一层的第一个子节点加入到根节点下
rootNode.AppendChild(firstLevelElement1);
//初始化第二层的第一个子节点
XmlElement secondLevelElement11 = myXmlDoc.CreateElement("LiginTime");
//填充第二层的第一个子节点的值(InnerText)
secondLevelElement11.InnerText = DateTime.Now.ToString();
firstLevelElement1.AppendChild(secondLevelElement11);
myXmlDoc.Save(xmlFilePath);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static string GetUserNameLastLoginTime()
{
string xmlFilePath = System.IO.Directory.GetCurrentDirectory() + "\\myUserXml.xml";
bool IsXmlNode = false;
string LoginUser = string.Empty;
Hashtable htUser=new Hashtable();
try
{
//初始化一个xml实例
XmlDocument myXmlDoc = new XmlDocument();
//加载xml文件(参数为xml文件的路径)
myXmlDoc.Load(xmlFilePath);
//获得第一个姓名匹配的节点(SelectSingleNode):此xml文件的根节点
XmlNode rootNode = myXmlDoc.SelectSingleNode("Nodes");
//分别获得该节点的InnerXml和OuterXml信息
string innerXmlInfo = rootNode.InnerXml.ToString();
string outerXmlInfo = rootNode.OuterXml.ToString();
//获得该节点的子节点(即:该节点的第一层子节点)
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
//获得该节点的属性集合
XmlAttributeCollection attributeCol = node.Attributes;
//
//{
//获取属性名称与属性值
//string name = attri.Name;
//string value = attri.Value;
if(node.HasChildNodes)
{
XmlNode secondLevelNode1 = node.FirstChild;
string name = secondLevelNode1.Name;
string innerText = secondLevelNode1.InnerText;
htUser.Add(innerText, node.Attributes["Name"].Value);
}
//获取属性名称与属性值
//string name = attri.Name;
//string value = attri.Value;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
ArrayList akeys=new ArrayList(htUser.Keys);
akeys.Sort();
akeys.Reverse();
foreach (string s in akeys)
{
string a = s;
LoginUser = htUser[a].ToString();
break;
}
return LoginUser;
}
private static void ModifyXmlInformation(string xmlFilePath)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
XmlNode rootNode = myXmlDoc.FirstChild;
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
//修改此节点的属性值
if (node.Attributes["Description"].Value.Equals("Made in USA"))
{
node.Attributes["Description"].Value = "Made in HongKong";
}
}
//要想使对xml文件所做的修改生效,必须执行以下Save方法
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void AddXmlInformation(string xmlFilePath)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
//添加一个带有属性的节点信息
foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
{
XmlElement newElement = myXmlDoc.CreateElement("color");
newElement.InnerText = "black";
newElement.SetAttribute("IsMixed", "Yes");
node.AppendChild(newElement);
}
//保存更改
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void DeleteXmlInformation(string xmlFilePath)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
{
//记录该节点下的最后一个子节点(简称:最后子节点)
XmlNode lastNode = node.LastChild;
//删除最后子节点下的左右子节点
lastNode.RemoveAll();
//删除最后子节点
node.RemoveChild(lastNode);
}
//保存对xml文件所做的修改
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;
namespace RDCJ3Client.Util
{
class LoginUser
{
public LoginUser()
{ }
public static void CreateXml(string User_id)
{
string CreateFilePath = System.IO.Directory.GetCurrentDirectory() + "\\myUserXml.xml";
if (System.IO.File.Exists(CreateFilePath))
{
//遍历xml文件的信息
GetXMLInformation(CreateFilePath, User_id);
}
else
{
//生成xml文件
GenerateXMLFile(CreateFilePath, User_id);
}
//修改xml文件的信息
ModifyXmlInformation(CreateFilePath);
//向xml文件添加节点信息
AddXmlInformation(CreateFilePath);
//删除指定节点信息
DeleteXmlInformation(CreateFilePath);
}
private static void GenerateXMLFile(string xmlFilePath, string User_id)
{
try
{
//初始化一个xml实例
XmlDocument myXmlDoc = new XmlDocument();
XmlDeclaration xmlDeclare = myXmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
//创建xml的根节点
XmlElement rootElement = myXmlDoc.CreateElement("Nodes");
//将根节点加入到xml文件中(AppendChild)
myXmlDoc.AppendChild(rootElement);
//初始化第一层的第一个子节点
XmlElement firstLevelElement1 = myXmlDoc.CreateElement("User");
//填充第一层的第一个子节点的属性值(SetAttribute)
firstLevelElement1.SetAttribute("Name", "" + User_id + "");
//将第一层的第一个子节点加入到根节点下
rootElement.AppendChild(firstLevelElement1);
//初始化第二层的第一个子节点
XmlElement secondLevelElement11 = myXmlDoc.CreateElement("LiginTime");
//填充第二层的第一个子节点的值(InnerText)
secondLevelElement11.InnerText = DateTime.Now.ToString();
firstLevelElement1.AppendChild(secondLevelElement11);
//将xml文件保存到指定的路径下
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void GetXMLInformation(string xmlFilePath, string User_id)
{
bool IsXmlNode = false;
try
{
//初始化一个xml实例
XmlDocument myXmlDoc = new XmlDocument();
//加载xml文件(参数为xml文件的路径)
myXmlDoc.Load(xmlFilePath);
//获得第一个姓名匹配的节点(SelectSingleNode):此xml文件的根节点
XmlNode rootNode = myXmlDoc.SelectSingleNode("Nodes");
//分别获得该节点的InnerXml和OuterXml信息
string innerXmlInfo = rootNode.InnerXml.ToString();
string outerXmlInfo = rootNode.OuterXml.ToString();
//获得该节点的子节点(即:该节点的第一层子节点)
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
//获得该节点的属性集合
XmlAttributeCollection attributeCol = node.Attributes;
//
//{
//获取属性名称与属性值
//string name = attri.Name;
//string value = attri.Value;
if (node.Attributes["Name"].Value.Equals(User_id) && node.HasChildNodes)
{
XmlNode secondLevelNode1 = node.FirstChild;
string name = secondLevelNode1.Name;
string innerText = secondLevelNode1.InnerText;
secondLevelNode1.InnerText = DateTime.Now.ToString();
IsXmlNode = true;
myXmlDoc.Save(xmlFilePath);
}
//}
判断此节点是否还有子节点
//if (node.HasChildNodes)
//{
// //获取该节点的第一个子节点
// XmlNode secondLevelNode1 = node.FirstChild;
// //获取该节点的名字
// string name = secondLevelNode1.Name;
// //获取该节点的值(即:InnerText)
// string innerText = secondLevelNode1.InnerText;
// //获取该节点的第二个子节点(用数组下标获取)
// //XmlNode secondLevelNode2 = node.ChildNodes[1];
// //name = secondLevelNode2.Name;
// //innerText = secondLevelNode2.InnerText;
// //Console.WriteLine("{0} = {1}", name, innerText);
//}
}
if (!IsXmlNode)
{
//初始化第一层的第一个子节点
XmlElement firstLevelElement1 = myXmlDoc.CreateElement("User");
//填充第一层的第一个子节点的属性值(SetAttribute)
firstLevelElement1.SetAttribute("Name", "" + User_id + "");
//将第一层的第一个子节点加入到根节点下
rootNode.AppendChild(firstLevelElement1);
//初始化第二层的第一个子节点
XmlElement secondLevelElement11 = myXmlDoc.CreateElement("LiginTime");
//填充第二层的第一个子节点的值(InnerText)
secondLevelElement11.InnerText = DateTime.Now.ToString();
firstLevelElement1.AppendChild(secondLevelElement11);
myXmlDoc.Save(xmlFilePath);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static string GetUserNameLastLoginTime()
{
string xmlFilePath = System.IO.Directory.GetCurrentDirectory() + "\\myUserXml.xml";
bool IsXmlNode = false;
string LoginUser = string.Empty;
Hashtable htUser=new Hashtable();
try
{
//初始化一个xml实例
XmlDocument myXmlDoc = new XmlDocument();
//加载xml文件(参数为xml文件的路径)
myXmlDoc.Load(xmlFilePath);
//获得第一个姓名匹配的节点(SelectSingleNode):此xml文件的根节点
XmlNode rootNode = myXmlDoc.SelectSingleNode("Nodes");
//分别获得该节点的InnerXml和OuterXml信息
string innerXmlInfo = rootNode.InnerXml.ToString();
string outerXmlInfo = rootNode.OuterXml.ToString();
//获得该节点的子节点(即:该节点的第一层子节点)
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
//获得该节点的属性集合
XmlAttributeCollection attributeCol = node.Attributes;
//
//{
//获取属性名称与属性值
//string name = attri.Name;
//string value = attri.Value;
if(node.HasChildNodes)
{
XmlNode secondLevelNode1 = node.FirstChild;
string name = secondLevelNode1.Name;
string innerText = secondLevelNode1.InnerText;
htUser.Add(innerText, node.Attributes["Name"].Value);
}
//获取属性名称与属性值
//string name = attri.Name;
//string value = attri.Value;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
ArrayList akeys=new ArrayList(htUser.Keys);
akeys.Sort();
akeys.Reverse();
foreach (string s in akeys)
{
string a = s;
LoginUser = htUser[a].ToString();
break;
}
return LoginUser;
}
private static void ModifyXmlInformation(string xmlFilePath)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
XmlNode rootNode = myXmlDoc.FirstChild;
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
foreach (XmlNode node in firstLevelNodeList)
{
//修改此节点的属性值
if (node.Attributes["Description"].Value.Equals("Made in USA"))
{
node.Attributes["Description"].Value = "Made in HongKong";
}
}
//要想使对xml文件所做的修改生效,必须执行以下Save方法
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void AddXmlInformation(string xmlFilePath)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
//添加一个带有属性的节点信息
foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
{
XmlElement newElement = myXmlDoc.CreateElement("color");
newElement.InnerText = "black";
newElement.SetAttribute("IsMixed", "Yes");
node.AppendChild(newElement);
}
//保存更改
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void DeleteXmlInformation(string xmlFilePath)
{
try
{
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlFilePath);
foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
{
//记录该节点下的最后一个子节点(简称:最后子节点)
XmlNode lastNode = node.LastChild;
//删除最后子节点下的左右子节点
lastNode.RemoveAll();
//删除最后子节点
node.RemoveChild(lastNode);
}
//保存对xml文件所做的修改
myXmlDoc.Save(xmlFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}