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>

  • 相关阅读:
    Oracle 左连接、右连接、全外连接、(+)号作用
    ORA-01940:无法删除当前已链接的用户(转)
    博客正式开通
    python扫描内网存活主机(scapy与nmap模块)
    Python学习之扫描网段主机与开启端口(避坑)
    Python学习之服务器与客户端的交互
    python渗透测试编程之kali linux2的AptanaStudio3安装
    Shellcodeloader免杀过火绒
    Cracer之Waf绕过
    30 Day Challenge Day 16 | Leetcode 692. Top K Frequent Words
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1249496.html
Copyright © 2011-2022 走看看