请高手帮我用c#写一个像下面格式的XML文件: <?xml version="1.0" encoding="GBK" ?>
- <result> <authentication>true</authentication> <userrole>student</userrole> </result>
- <result> <authentication>true</authentication> <userrole>student</userrole> </result>
解决方案 »
- winform中出现:本机框架位于调用堆栈之上 因此无法计算表达式的值
- js 获取treeview节点的Text和value值
- 谁留个完整正规的项目需求分析设计,好让我走上正路,最好能出现用例图什么之类的
- 把DLL的复制到本地设为FALSE后又出现的问题?
- 如何在datagrid中实现主表与子表的效果
- dataGridView显示日期不完整
- 网络连接失败问题!
- C#中的ACCESS连接问题
- 百度排名批量查询工具,求思路,或者给个详细demo,谢谢
- 如何保存C1 Flexgrid的样式?
- 关于哈希表的 GETLOWBOUND 和GETUPPERBOUND
- 如何判断浏览器版本?是否开启了javascript?
xw.WriteStartElement("result")
xw.WriteElementString("authentication","true");
xw.WriteElementString("userrole","student");
xw.WriteEndElement();xw.Flush();
xw.Close();
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();
}
文件内容:<?xml version="1.0" encoding="gb2312"?>
<result>
<authentication>true</authentication>
<userrole>student</userrole>
</result>
#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
#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
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);
}