C#创建xml文件并写入内容
2016-12-08 10:49
1471 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace xml
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
//创建Xml声明部分,即<?xml version="1.0" encoding="utf-8" ?>
XmlDeclaration Declaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
//创建根节点
XmlNode rootNode = xmlDoc.CreateElement("root");
//创建student子节点
XmlNode testPointNode = xmlDoc.CreateElement("testPoint");
//创建一个属性
XmlAttribute testPointID = xmlDoc.CreateAttribute("TestPointID");
testPointID.Value = "1";
XmlAttribute caseID = xmlDoc.CreateAttribute("CaseId");
caseID.Value = "TEST_0001";
XmlAttribute name = xmlDoc.CreateAttribute("Name");
name.Value = "111";
//xml节点附件属性
testPointNode.Attributes.Append(testPointID);
testPointNode.Attributes.Append(caseID);
testPointNode.Attributes.Append(name);
rootNode.AppendChild(testPointNode);
//附加根节点
xmlDoc.AppendChild(rootNode);
xmlDoc.InsertBefore(Declaration, xmlDoc.DocumentElement);
//保存Xml文档
xmlDoc.Save(@"d:\testPoint.xml");
Console.WriteLine("已保存Xml文档");
}
}
}
转自:http://blog.sina.com.cn/s/blog_6cc1c52d01012lc3.html
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace xml
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
//创建Xml声明部分,即<?xml version="1.0" encoding="utf-8" ?>
XmlDeclaration Declaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
//创建根节点
XmlNode rootNode = xmlDoc.CreateElement("root");
//创建student子节点
XmlNode testPointNode = xmlDoc.CreateElement("testPoint");
//创建一个属性
XmlAttribute testPointID = xmlDoc.CreateAttribute("TestPointID");
testPointID.Value = "1";
XmlAttribute caseID = xmlDoc.CreateAttribute("CaseId");
caseID.Value = "TEST_0001";
XmlAttribute name = xmlDoc.CreateAttribute("Name");
name.Value = "111";
//xml节点附件属性
testPointNode.Attributes.Append(testPointID);
testPointNode.Attributes.Append(caseID);
testPointNode.Attributes.Append(name);
rootNode.AppendChild(testPointNode);
//附加根节点
xmlDoc.AppendChild(rootNode);
xmlDoc.InsertBefore(Declaration, xmlDoc.DocumentElement);
//保存Xml文档
xmlDoc.Save(@"d:\testPoint.xml");
Console.WriteLine("已保存Xml文档");
}
}
}
转自:http://blog.sina.com.cn/s/blog_6cc1c52d01012lc3.html
相关文章推荐
- C#创建xml文件并写入内容
- C# 创建Excel并写入内容
- C# 如何创建一个xml文件?并写入指定内容?
- C# 创建Excel并写入内容
- C#-XML文件提取字符串+字符串存为XML文件+创建XML(自定义节点)文件+读取节点内容
- C# 如何创建一个xml文件?并写入指定内容?
- C# 创建Excel并写入内容
- 在C#中如何创建写入XML文件示例
- 动态创建TXMLDocument对XML文件进行读取和写入
- c#读取xml文件的节点内容
- C#中创建、打开、读取、写入、保存Excel的一般性代码
- ASP.NET创建文件并写入内容
- ASP.NET创建文件并写入内容
- C#代码创建Xml文件
- c# 代码创建xml文件
- c#读取word内容,c#写入word内容,C#操作word
- Asp创建文件而且写入内容的函数
- c# 创建指定大小的空字符填充的文本文件 在指定位置读写相关内容
- 遍历现有XML文件,同时写入到新文件中. ---(c#)
- C#.NET向现有文件添加文本+创建一个新文本文件并写入一个字符串