XmlDocument();
            //定义XML文档头文件
            XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0","utf-8",null);
            
//增加XML文档头
            xmlDocument.AppendChild(xmlDeclaration);
            
//定义XML的根
            XmlElement xmlRoot = xmlDocument.CreateElement("userdata");
            
//添加根的属性
            xmlRoot.SetAttribute("createuser","true");
            
//修改根属性的值
            xmlRoot.GetAttributeNode("createuser").Value = "false";
            
//添加子节点并设置子节点属性
            xmlDocument.AppendChild(xmlRoot);
            XmlElement dataconnection 
= xmlDocument.CreateElement("dataconnection");
            XmlElement server 
= xmlDocument.CreateElement("server");
            server.InnerText 
= "localhost";
            XmlElement uid 
= xmlDocument.CreateElement("uid");
            uid.InnerText 
= "sa";
            XmlElement pwd 
= xmlDocument.CreateElement("pwd");
            xmlRoot.AppendChild(dataconnection);
            dataconnection.AppendChild(server);
            dataconnection.AppendChild(uid);
            dataconnection.AppendChild(pwd);
            
//保存XML文档
            xmlDocument.Save("book.xml");
            
//读取子节点server的值
            XmlNode xnserver = xmlDocument.SelectSingleNode("userdata/dataconnection/server");
            Console.WriteLine(
"node server's value is "+ xnserver.InnerText);
            Console.ReadLine();

生成的XML代码如下:

 


  <dataConnection>
    
<server>localhost</server>
    
<uid>sa</uid>
    
<pwd />
  
</dataConnection>
</userdata>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2021-11-07
猜你喜欢
  • 2022-12-23
  • 2021-08-14
  • 2022-03-09
相关资源
相似解决方案