创建一个XML文件
2016-11-17 20:38
232 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace 使用传统方法创建XML
{
class Program
{
static void Main(string[] args)
{
// 命名空间:System.XML;
// 类库:XmlDocument 文档
// XmlElement 元素
// XmlAttribute 属性
XmlDocument xdoc = new XmlDocument();
// 所有的元素使用文档节点创建
XmlDeclaration xdec = xdoc.CreateXmlDeclaration("1.0", "gb2312", null);
xdoc.AppendChild(xdec);
XmlElement xele = xdoc.CreateElement("root");
xdoc.AppendChild(xele);
XmlAttribute xAttr = xdoc.CreateAttribute("id");
xAttr.Value = "123456";
XmlText txt = xdoc.CreateTextNode("我是一个文本节点");
xele.AppendChild(txt);
xele.Attributes.Append(xAttr);
xdoc.Save("1.xml");
Console.WriteLine("创建成功!");
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace 使用传统方法创建XML
{
class Program
{
static void Main(string[] args)
{
// 命名空间:System.XML;
// 类库:XmlDocument 文档
// XmlElement 元素
// XmlAttribute 属性
XmlDocument xdoc = new XmlDocument();
// 所有的元素使用文档节点创建
XmlDeclaration xdec = xdoc.CreateXmlDeclaration("1.0", "gb2312", null);
xdoc.AppendChild(xdec);
XmlElement xele = xdoc.CreateElement("root");
xdoc.AppendChild(xele);
XmlAttribute xAttr = xdoc.CreateAttribute("id");
xAttr.Value = "123456";
XmlText txt = xdoc.CreateTextNode("我是一个文本节点");
xele.AppendChild(txt);
xele.Attributes.Append(xAttr);
xdoc.Save("1.xml");
Console.WriteLine("创建成功!");
Console.ReadKey();
}
}
}
相关文章推荐
- 创建一个xml文件(c:/test.html),为该文件增加三个节点,,,将b的attribute改为4并保存
- 一个经过改良的XMLHelper(包含了序列化,反序列化,创建xml文件,读取节点,C#对象与xml文件的相互转换等等。)
- dom4j 创建一个带命名空间的pom.xml 文件
- 判断XML文件是否存在,不存在就创建一个XMl文件,存在就在里面添加一条数据
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- 如何在Android Studio中创建一个selector.xml文件
- C# 如何创建一个xml文件?并写入指定内容?
- 用xmldocument创建一个Xml文件
- PHP 通过DOM方式创建一个book.xml文件
- 使用dom4j创建一个xml文件
- 用JS创建一个XML文件
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
- C# 如何创建一个xml文件?并写入指定内容?
- GTK+-3.0编程 (五) GtkBuilder用于快速创建界面,它读取一个xml文件
- PHP DOM创建生成一个XML文件
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
- 用JS创建一个XML文件
- PHP 通过DOM方式创建一个book.xml文件
- 用JS创建一个XML文件
- 用xmldocument创建一个Xml文件