C#之XML基础 为一个节点添加两个属性值
2017-10-30 16:16
267 查看
1、代码
[csharp] view
plain copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
//创建第一行描述信息
// 版本 编码格式
XmlDeclaration theFirstRowOfXml= xml.CreateXmlDeclaration("1.0","utf-8",null);
xml.AppendChild(theFirstRowOfXml);
//保存之前要创建根节点,根节点的名字是Books
XmlElement Books= xml.CreateElement("Books");
//再把创建的根节点加入到xml中
xml.AppendChild(Books);
//创建Books根节点下的一个子节点
XmlElement Book1 = xml.CreateElement("Book");
Books.AppendChild(Book1);
XmlElement Name = xml.CreateElement("Name");
Name.InnerText = "金刚经";
Book1.AppendChild(Name);
XmlElement Class = xml.CreateElement("Class");
Class.InnerText ="佛家";
Book1.AppendChild(Class);
XmlElement Count = xml.CreateElement("Count");
Count.SetAttribute("count", "10");
Count.SetAttribute("discount","80%");
Book1.AppendChild(Count);
xml.Save("创建的文件.xml");
Console.WriteLine("OK");
Console.ReadKey();
}
}
}
2、控制台效果
3、XML文件
[csharp] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<Books>
<Book>
<Name>金刚经</Name>
<Class>佛家</Class>
<Count count="10" discount="80%" />
</Book>
</Books>
[csharp] view
plain copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
//创建第一行描述信息
// 版本 编码格式
XmlDeclaration theFirstRowOfXml= xml.CreateXmlDeclaration("1.0","utf-8",null);
xml.AppendChild(theFirstRowOfXml);
//保存之前要创建根节点,根节点的名字是Books
XmlElement Books= xml.CreateElement("Books");
//再把创建的根节点加入到xml中
xml.AppendChild(Books);
//创建Books根节点下的一个子节点
XmlElement Book1 = xml.CreateElement("Book");
Books.AppendChild(Book1);
XmlElement Name = xml.CreateElement("Name");
Name.InnerText = "金刚经";
Book1.AppendChild(Name);
XmlElement Class = xml.CreateElement("Class");
Class.InnerText ="佛家";
Book1.AppendChild(Class);
XmlElement Count = xml.CreateElement("Count");
Count.SetAttribute("count", "10");
Count.SetAttribute("discount","80%");
Book1.AppendChild(Count);
xml.Save("创建的文件.xml");
Console.WriteLine("OK");
Console.ReadKey();
}
}
}
2、控制台效果
3、XML文件
[csharp] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<Books>
<Book>
<Name>金刚经</Name>
<Class>佛家</Class>
<Count count="10" discount="80%" />
</Book>
</Books>
相关文章推荐
- PHP中开发XML应用程序之基础篇 添加节点 删除节点 查询节点 查询节
- C# 添加,修改,删除Xml节点
- 如何利用C#在XML 中添加 节点得到如下的格式 插3根鸡毛了
- 【JAVA基础】0012--------判断String类型的XML中是否存在某一个节点
- C# 添加xml节点多了xmlns属性问题
- C#中对 XML节点进行添加,删除,查找和删除操作
- C# 添加xml的节点
- 给出两个表示两个非负整数的非空链表。数字以相反的顺序存储,它们的每个节点都包含一个数字。添加两个数字,并将其作为链接列表返回。
- C# XML 给xml文件添加根节点
- C# XML 添加,修改,删除Xml节点
- C#中为XML节点添加属性(转)
- PHP中开发XML应用程序之基础篇 添加节点 删除节点 查询节点 查询节
- C# 添加,修改,删除Xml节点
- C# XML 给一个元素添加带有内容的子元素
- 将两个不同格式的XML文件,进行节点对照,并生成一个用于对照功能的XML
- JavaWeb基础 通过设置web.xml 实现一个servlet拥有两个浏览路径
- C#中对 XML节点进行添加,删除,查找和删除操作
- C# 添加、修改、删除xml节点例子
- C# 读取与修改xml一个节点的值
- 将两个不同格式的XML文件,进行节点对照,并生成一个用于对照功能的XML