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
>
查看全文
相关阅读:
四个例子实战讲解.htaccess文件rewrite规则(转)
unserialize反序列化错误的解决办法
tp框架--------where("1")
jq 鼠标点击跳转页面后 改变点击菜单的样式代码
jq不懂的地方
js产生随机数的几个方法
js邮箱,汉字,数字 表单验证
js&jQ判断checkbox表单是否被选中
绝对好用Flash多文件大文件上传控件
CKeditor从Word粘贴格式问题
原文地址:https://www.cnblogs.com/RuiLei/p/647303.html
最新文章
EIGRP知识汇总
class-map and policy-map config
ip prefix-list应用
Cisco PBR Case2
Cisco PBR Case1
Juniper EX系列交换机堆叠配置
IP Event Dampening(IP事件惩罚)
RIPv1和RIPv2区别
阿里云Linux启动tomcat并能外网访问
SQL Server 预编译执行SQLs
热门文章
ASP.NET Code First Update-Database
MVC5 ModelState
MVC5 WebAPI 跨域处理
修改SVN路径
jQuery DataTable 删除数据后重新加载
SwaggerUI ASP.Net WebAPI2
maven工程开始
新建Maven工程
手机移动端可滚动导航代码
PHP————系统常量
Copyright © 2011-2022 走看看