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>

  • 相关阅读:
    vs2008 服务器控件库
    dropdoenlist 设置默认值
    .NET用HttpFileCollection实现多文件上传
    dropdownlist 绑定
    简单的webservice调用(天气预报)
    统计在线人数
    多文件上传
    dropdownlist1 绑定 value值
    WMI 脚本入门:第一部分 (MSDN)
    WMI 脚本入门:第三部分 (MSDN)
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1249496.html
Copyright © 2011-2022 走看看