C#添加xml文件

引用:System.Xml;

XmlDocument doc = new XmlDocument(); 
XmlElement Root = doc.CreateElement("Root");//主内容
doc.AppendChild(Root);
XmlElement Child1 = doc.CreateElement("attr1");
XmlAttribute attr1=  doc.CreateAttribute("attr1");
attr1.Value = "arrt1Content";
Child1.Attributes.Append(attr1);
Root.AppendChild(Child1);

//这一行和上面顺序不能反
//arr1就你的字段,如字段中有引号就要用\' ,最好不要用xml 的text段存内容
//如果你有170 你的循环要对 应该有两个循环 一个在attr1 这 用于添加150个字段 一个在child1 用于添加几行
// doc.InnerXml  这个属性就是你的xml 内容

doc.Save("c://1.xml");//保存这个xml 网页或exe 都可以
View Code

 

XmlDocument xmldoc = new XmlDocument();
//声明节
XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.AppendChild(dec);
//加入一个根节点
XmlElement oneNode = xmldoc.CreateElement("CATALOG");
//创建节点
XmlElement twoNode = xmldoc.CreateElement("CD");
XmlElement twoNodeone = xmldoc.CreateElement("TITLE");
twoNodeone.InnerText="Empire Burlesque";
twoNode.AppendChild(twoNodeone);//添加到CD节点下面
oneNode.AppendChild(twoNode1);//添加到CATALOG节点下面
xmldoc.AppendChild(oneNode);
xmldoc.Save(Server.MapPath("")+"/1.xml");//保存xml 
View Code

 

XPath 可用来在 XML 文档中对元素和属性进行遍历。但是它不能用来创建 xml ,创建 xml一般使用XmlWriter、XmlDocument或是linq2xml 之类的。
/// <summary> 方法说明.....创建XML文档
        /// </summary>
        /// <param name="xmlFile">要保存的文档名,不需要添加后缀</param>
       private void fabao_Senior(string xmlFile)
        {
            XmlDocument xdoc = new XmlDocument(); // 创建 xml 文档对象
            XmlNode xnode = xdoc.CreateXmlDeclaration("1.0", "GB2312", "yes");
            xdoc.AppendChild(xnode);
 
            /*创建的时候最好把所有 节点元素 都添加进去 /*创建根节点*/
            XmlElement rootEle = xdoc.CreateElement("", "SysString", "");
            xdoc.AppendChild(rootEle);
 
            // // 查找 SysString 根节点
            XmlNode roots = xdoc.SelectSingleNode("SysString");
 
            XmlElement Total = xdoc.CreateElement("toTable");
 
            /*创建 FileName 节点元素*/
            XmlElement GameName = xdoc.CreateElement("Name");
            /*...参见上面的 Total.SetAttribute()...*/
            GameName.InnerText = "Null";
            Total.AppendChild(GameName);
 
            XmlElement Name = xdoc.CreateElement("Age");
            Name.InnerText = "Null";
            Total.AppendChild(Name);
 
            /*☆☆☆☆☆☆☆☆☆☆☆☆☆☆*/
 
            for (int i = 65; i <= 90; i++)
            {
                char q = (char)i;
                XmlElement eleName = xdoc.CreateElement("char_" + q);
                eleName.InnerText = "true/false";
                Total.AppendChild(eleName);
            }
 
            for (int i = 97; i <= 122; i++)
            {
                char q = (char)i;
                XmlElement eleName = xdoc.CreateElement("char_" + q);
                eleName.InnerText = "true/false";
                Total.AppendChild(eleName);
            }
 
            /*把 Total 包含在 根节点末尾*/
            roots.AppendChild(Total);
 
            //保存创建好的XML文档
            xdoc.Save(xmlFile + ".xml");   // 保存文件
        }
View Code
XmlTextWriter对象包含了很多可用于在创建XML文件时添加元素和属性到XML文件里的方法,比较重要的有:


◆WriteStartDocument()-创建XML文件首先就需要用到这个方法,它是在创建XML文件的第一行代码,用来指定该文件是XML文件以及设置它的编码类型;

◆WriteStartElement(string)-这个方法的作用是在XML文件中创建新元素,你可以通过String参数设置元素的名称(当然了,你还可以使用optional关键字指定一个可选的参数);

◆WriteElementString(name, text_value)-如果你需要创建一个除了字符,什么也没有的(如不嵌套元素)的元素,你可以使用该方法;

◆WriteEndElement()-对应WriteStartElement(string)方法,作为一个元素的结尾;

◆WriteEndDocument()-XML文件创建完成后使用该方法结束;

◆Close()-关闭所有的文本流,把创建的XML文件输出到指定位置。

使用XmlTextWriter对象创建XML文件,需要在类构造器中指定文件的类型,而且编码类型必须是System.Text.Encoding,如:System.Text.Encoding.ASCII, System.Text.Encoding.Unicode及System.Text.Encoding.UTF8,在XmlTextWriter类构造器指定为何种类型,在输出XML文件将以那种流文件形式输出。
C#创建XML文件之使用XmlTextWriter对象创建一个简单的XML文件:
XmlTextWriter writer=new XmlTextWriter(Server.MapPath("phone4.xml"),null); 
 writer.Formatting = Formatting.Indented; //缩进格式 
 writer.Indentation =4; 
首先我们要注意是否有导入System.Xml and System.Text命名空间,
然后我们在Page_Load事件中创建一个XmlTextWriter对象实例,
并且指定创建的XML文件保存为userInfo.xml文件和它的编码类型为UTF8(a translation of 16-bit unicode encoding into 8-bits),
然后使用WriteStartElement(elementName)方法来创建嵌套了其他元素的元素,并以WriteEndElement()作为结束,
此外,我们使用WriteElementString(elementName, textValue)方法来创建最底层即没有嵌套其他元素的元素。
View Code
 
 System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
         
        System.Xml.XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
        xml.AppendChild(dec);
        System.Xml.XmlElement ele = xml.CreateElement("A");
        xml.AppendChild(ele);
        System.Xml.XmlElement ele2 = xml.CreateElement("B");
         
        System.Xml.XmlAttribute xa = xml.CreateAttribute("C");
        xa.Value = "D";
        ele2.Attributes.Append(xa);
        ele2.InnerXml= "E";
        ele.AppendChild(ele2);
 
        xml.Save(Server.MapPath("~/1.xml"));
View Code

学习网站:http://www.bccn.net/Article/web/xml/


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值