zoukankan      html  css  js  c++  java
  • c# 代码创建xml文件

     #region /***创建文件***/
            
    public void   CreateXmlFile(string strFilePath,string[] strElement,string[] strElementString)
            
    {    
                
    try
                
    {
                    XmlDocument xmldoc 
    = new XmlDocument( ) ;
                    XmlDeclaration   xn   
    =  xmldoc.CreateXmlDeclaration("1.0","gb2312",null);   
                    xmldoc.AppendChild(xn);
        
                    
    //加入一个根元素
                    XmlElement  xmlelem = xmldoc.CreateElement ( "" , "myinfo" , "" ) ;
                    xmldoc.AppendChild(xmlelem) ;
                    XmlElement xe1
    =xmldoc.CreateElement("user"); 
       
                    XmlElement xesub2
    =xmldoc.CreateElement(strElement[0].ToString());
                    xesub2.InnerText
    =strElementString[0].ToString();
                    xe1.AppendChild(xesub2);
            
                    XmlElement xesub3
    =xmldoc.CreateElement(strElement[1].ToString());
                    xesub3.InnerText
    =strElementString[1].ToString();
                    xe1.AppendChild(xesub3);

                    xmldoc.ChildNodes.Item(
    1).AppendChild (xe1) ;
     
                    
    //保存创建好的XML文档
                    try
                    
    {
                        xmldoc.Save(strFilePath) ; 
                    }

                    
    catch (Exception e )
                    
    {
                        
    throw e;
                    }

                    
        
                }

                
    catch (Exception err)
                
    {
                    
    throw err;    
                }

                
            }


            
    #endregion

    给xml添加"<?xml version="1.0" encoding="gb2312"?> "格式:
    XmlDocument xmldoc = new XmlDocument( ) ;
        XmlDeclaration   xn   =  xmldoc.CreateXmlDeclaration("1.0","gb2312",null);  
        xmldoc.AppendChild(xn);

    最后创建好的xml文件:
    <?xml version="1.0" encoding="gb2312"?>
    <myinfo>
      <user>
       ***
      </user>
    </myinfo>

  • 相关阅读:
    在桌面创建robotframework Ride的快捷方式启动RIDE
    RIDE 接口自动化请求体参数中文时报错:“UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 9......”
    下拉框选择
    Python下安装MySQLdb模块
    出现 Request Entity Too Large问题的解决方法
    Centos配置nginx反向代理8090端口到80端口
    超简单Centos+Docker+Halo搭建java向博客
    JAVA使用POI如何导出百万级别数据
    Mybatis的MapperRegistry错误
    Navicat连接mysql8出现1251错误
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1249496.html
Copyright © 2011-2022 走看看