110,536
社区成员
发帖
与我相关
我的任务
分享
private void button2_Click(object sender, EventArgs e)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = Encoding.GetEncoding("gbk");//只能得到 gb2312
XmlWriter writer = XmlWriter.Create("test.xml", settings);
writer.WriteStartDocument();
writer.WriteStartElement("result");
writer.WriteStartElement("authentication");
writer.WriteString("true");
writer.WriteEndElement();
writer.WriteStartElement("userrole");
writer.WriteString("student");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
string path = Application.StartupPath + "\\MyXml.xml";
if (!File.Exists(path)) //不存在文件创建
{
XmlDocument xml = new XmlDocument();
XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
xml.AppendChild(dec);
XmlNode rootnode = xml.CreateNode(XmlNodeType.Element, "result", "");
xml.AppendChild(rootnode); //添加跟节点
XmlNode childnode1 = xml.CreateNode(XmlNodeType.Element, "authentication", "");
childnode1.InnerText = "true";
rootnode.AppendChild(childnode1);
XmlNode childnode2 = xml.CreateNode(XmlNodeType.Element, "userrole", "");
childnode2.InnerText = "student";
rootnode.AppendChild(childnode2);
xml.Save(path);
}
#region 添加节点
public static void AddXmlNode(string text, string value, string fileName)
{
XmlDocument xmldoc = new XmlDocument();
XmlElement root;
xmldoc.Load( fileName);
root = xmldoc.DocumentElement;
XmlElement subNode = xmldoc.CreateElement("subNode");
XmlElement nodeName = xmldoc.CreateElement("nodeName");
XmlElement nodeTag = xmldoc.CreateElement("nodeTag");
nodeName.InnerXml = text;
nodeTag.InnerXml = value;
subNode.AppendChild(nodeName);
subNode.AppendChild(nodeTag);
root.AppendChild(subNode);
xmldoc.Save( fileName);
}
#endregion
#region 删除节点
public static void DelXmlNode(string text, string value, string fileName)
{
XmlDocument xmldoc = new XmlDocument();
XmlElement root;
xmldoc.Load(fileName);
root = xmldoc.DocumentElement;
foreach (XmlNode item in root.ChildNodes)
{
if (item.ChildNodes[0].InnerXml==text&&item.ChildNodes[1].InnerXml==value)
{
root.RemoveChild(item);
break;
}
}
xmldoc.Save(fileName);
}
#endregion
#region 创建
public static bool CreatXml(string fileName)
{
try
{
if (!File.Exists( fileName))
{
XmlTextWriter textWriter = new XmlTextWriter(fileName, null);
//-------创建XML写操作对象
textWriter.Formatting = Formatting.Indented;
//-------开始写过程,调用WriteStartDocument方法
textWriter.WriteStartDocument();
//-------写入说明
//textWriter.WriteComment("this XML is created from a tree");
//-------添加第一个根节点
textWriter.WriteStartElement("rootNode");
textWriter.WriteEndElement();
//------ 写文档结束,调用WriteEndDocument方法
textWriter.WriteEndDocument();
//-----关闭输入流
textWriter.Close();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return true;
}
#endregion
#region 写入
public static bool WriteXml(string key, string value, string fileName)
{
XmlDocument xmldoc = new XmlDocument();
XmlElement root;
try
{
xmldoc.Load( fileName);
root = xmldoc.DocumentElement;
foreach (XmlNode item in root.ChildNodes)
{
if (item.ChildNodes[0].InnerXml==key)
{
item.ChildNodes[1].InnerXml = value;
break;
}
}
xmldoc.Save( fileName);
}
catch (System.Exception ex)
{
throw new Exception(ex.Message);
}
return true;
}
#endregion
#region 读取
public static string ReadXml(string keys, string fileName)
{
XmlDocument xmldoc = new XmlDocument();
XmlElement root;
xmldoc.Load(fileName);
root = xmldoc.DocumentElement;
foreach (XmlNode item in root.ChildNodes)
{
if (item.ChildNodes[0].InnerXml == keys)
{
return item.ChildNodes[1].InnerXml;
}
}
return "";
}
#endregion