XmlDocument的应用---创建Xml模板 (转)
来源:互联网 发布:网络普通发票管理系统 编辑:程序博客网 时间:2024/06/05 12:44
protected void btnCreate_Click(object sender, EventArgs e)
{
//定义XMLDocument
XmlDocument xmlDocument = new XmlDocument();
//定义XML文档头文件
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
//增加XML文档头
xmlDocument.AppendChild(xmlDeclaration);
//定义XML的根
XmlElement xmlRoot = xmlDocument.CreateElement("Roots");
//添加XML的根
xmlDocument.AppendChild(xmlRoot);
//添加根的属性
xmlRoot.SetAttribute("RootAttribute", "Value");
//修改根属性的值
xmlRoot.GetAttributeNode("RootAttribute").Value = "FixValue";
//定义节点
XmlNode xmlElement;
//循环创建节点
for (int i = 0; i < 2; i++)
{
//创建XML根的节点
xmlElement = xmlDocument.CreateElement("Element");
//XML需要的属性列表
foreach (KeyValuePair<String, String> keyValuePair in NameValueDictionary())
{
//定义XML根的节点中的属性
XmlAttribute oneAttribute = xmlDocument.CreateAttribute(keyValuePair.Key);
oneAttribute.Value = keyValuePair.Value;
XmlAttribute secAttribute = xmlDocument.CreateAttribute(keyValuePair.Key);
secAttribute.Value = keyValuePair.Value;
//添加XML根的节点中的属性
xmlElement.Attributes.Append(oneAttribute);
xmlElement.Attributes.Append(secAttribute);
//添加XML根的节点
xmlRoot.AppendChild(xmlElement);
}
}
//保存XML文档
xmlDocument.Save(Server.MapPath("OutDocument.XML"));
}
//XML需要的属性列表
private Dictionary<String, String> NameValueDictionary()
{
Dictionary<String, String> nameValueDictionary = new Dictionary<String, String>();
nameValueDictionary.Add("AttributeOne", "One");
nameValueDictionary.Add("AttributeSec", "Second");
return nameValueDictionary;
}
{
//定义XMLDocument
XmlDocument xmlDocument = new XmlDocument();
//定义XML文档头文件
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
//增加XML文档头
xmlDocument.AppendChild(xmlDeclaration);
//定义XML的根
XmlElement xmlRoot = xmlDocument.CreateElement("Roots");
//添加XML的根
xmlDocument.AppendChild(xmlRoot);
//添加根的属性
xmlRoot.SetAttribute("RootAttribute", "Value");
//修改根属性的值
xmlRoot.GetAttributeNode("RootAttribute").Value = "FixValue";
//定义节点
XmlNode xmlElement;
//循环创建节点
for (int i = 0; i < 2; i++)
{
//创建XML根的节点
xmlElement = xmlDocument.CreateElement("Element");
//XML需要的属性列表
foreach (KeyValuePair<String, String> keyValuePair in NameValueDictionary())
{
//定义XML根的节点中的属性
XmlAttribute oneAttribute = xmlDocument.CreateAttribute(keyValuePair.Key);
oneAttribute.Value = keyValuePair.Value;
XmlAttribute secAttribute = xmlDocument.CreateAttribute(keyValuePair.Key);
secAttribute.Value = keyValuePair.Value;
//添加XML根的节点中的属性
xmlElement.Attributes.Append(oneAttribute);
xmlElement.Attributes.Append(secAttribute);
//添加XML根的节点
xmlRoot.AppendChild(xmlElement);
}
}
//保存XML文档
xmlDocument.Save(Server.MapPath("OutDocument.XML"));
}
//XML需要的属性列表
private Dictionary<String, String> NameValueDictionary()
{
Dictionary<String, String> nameValueDictionary = new Dictionary<String, String>();
nameValueDictionary.Add("AttributeOne", "One");
nameValueDictionary.Add("AttributeSec", "Second");
return nameValueDictionary;
}
生成的XML模块
<?xml version="1.0" encoding="utf-8"?>
<Roots RootAttribute="FixValue">
<Element AttributeOne="One" AttributeSec="Second" />
<Element AttributeOne="One" AttributeSec="Second" />
</Roots>
<?xml version="1.0" encoding="utf-8"?>
<Roots RootAttribute="FixValue">
<Element AttributeOne="One" AttributeSec="Second" />
<Element AttributeOne="One" AttributeSec="Second" />
</Roots>
- XmlDocument的应用---创建Xml模板 (转)
- 用XmlDocument创建XML文档
- 用XmlDocument创建XML文档
- 用XmlDocument创建XML文档
- 用XmlDocument创建XML文档
- 用XmlDocument创建XML文档
- XmlDocument,XmlNode,XmlElement创建复杂XML文档
- XmlDocument和LINQ to XML两种方式对xml的创建、查询
- XMLTextReader和XmlDocument读取XML文件的比较(转)
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例
- 使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例(转载)
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例(转载)
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例
- 用XmlDocument的处理XML文件
- XMLDocument与XML的区别和联系
- XMLDocument 最简单的读取xml例子
- 创建跨浏览器Javascript的XMLDocument对象
- 创建跨浏览器Javascript的XMLDocument对象
- android2.0安装
- [zt]用xmpp架设自己的Openfire聊天服务器
- XMPP
- Android开发-新建Activity
- Java 获得天气信息
- XmlDocument的应用---创建Xml模板 (转)
- [ZT]openfire及其源码的开发和部署
- 【ZT】Subclipse[安装]
- 【ZT】subversion服务器搭建
- Office2010安装错误1402问题
- [ZT] WIN-Subversion配置安装教程
- 搭建subversion时候遇到的问题以及解决方案
- GBK不可映射的字符&&javac找不到main问题
- PS如何不失真压缩图片【网站上应用】