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
>
查看全文
相关阅读:
JavaScript实现html购物车代码
SQLServer2019安装教程
SQLServer2019安装教程
pragma指令简介
16进制字符串转数字(C/C++,VB/VB.net,C#)
对方网络非正常断开检测方法
stl map高效遍历删除的方法
memmove 和 memcpy的区别
Socket 心跳包机制总结
如何将内存中的位图数据绘制在DC上
原文地址:https://www.cnblogs.com/RuiLei/p/647303.html
最新文章
汉语-词语辨析:本质与现象
signal
shutdown
showmount
showkey
showfont
SHOW
shells
setserial
setquota
热门文章
setmetamode
shell之并行
cURL 学习笔记与总结(4)使用 cURL 从 ftp 上下载文件与上传文件到 ftp
Java实现 LeetCode 87 扰乱字符串
Java实现 LeetCode 87 扰乱字符串
Java实现 LeetCode 86 分割链表
Java实现 LeetCode 86 分割链表
Java实现 LeetCode 86 分割链表
JavaScript实现html购物车代码
JavaScript实现html购物车代码
Copyright © 2011-2022 走看看