zoukankan
html css js c++ java
XmlDocument的应用创建Xml模板
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;
}
生成的XML模块
<?
xml version="1.0" encoding="utf-8"
?>
<
Roots
RootAttribute
="FixValue"
>
<
Element
AttributeOne
="One"
AttributeSec
="Second"
/>
<
Element
AttributeOne
="One"
AttributeSec
="Second"
/>
</
Roots
>
查看全文
相关阅读:
python爬虫之urllib库(三)
python爬虫之urllib库(二)
认识CSS中css引入方式、选择器、文本样式设置以及显示模式
认识CSS中盒子模型
python爬虫之urllib库(一)
认识CSS中css背景样式设置
python爬虫之认识爬虫和爬虫原理
认识CSS中css的三大特性:层叠性、继承性以及优先级
认识HTML中表格、列表标签以及表单控件
python-基础学习篇(八)
原文地址:https://www.cnblogs.com/RuiLei/p/647303.html
最新文章
ibatas的一些实例及解释
一方库、二方库、三方库说明
String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)
求一个数字的长度
Java 比较两个字符串的大小
Java ftp断点续传
System.in.read()
文件操作
获取当前工程路径
字符与字节
热门文章
文件创建、删除、重名名文件、判断文件的读写权限以及是否存在,设置和查询文件的最近修改时间
Hibernate+jxl+excel导入数据库
Linux下查看CPU信息
寒假学习日报(八)
寒假学习日报(七)
寒假学习日报(六)
寒假学习日报(五)
寒假学习日报(四)
寒假学习日报(三)
寒假学习日报(二)
Copyright © 2011-2022 走看看