请高手帮我用c#写一个像下面格式的XML文件: <?xml version="1.0" encoding="GBK" ?> 
- <result>  <authentication>true</authentication>   <userrole>student</userrole>   </result>

解决方案 »

  1.   

    XmlWriter xw=XmlWriter("x.xml");
    xw.WriteStartElement("result")
    xw.WriteElementString("authentication","true");
    xw.WriteElementString("userrole","student");
    xw.WriteEndElement();xw.Flush();
    xw.Close();
      

  2.   


    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>
      

  3.   


     #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
      

  4.   


    #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
      

  5.   

    可以用序列化的方式生产xml啊。给实体类加标签属性啊。
      

  6.   


            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);
            }