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
>
查看全文
相关阅读:
IE7下总提示" 缺少标识符、字符串或数字"
#pragma 用法
破解win7开机密码
教你怎么样设计一块好的PCB板精华教程
Object reference not set to an instance of an object. 'Infinity' is not a valid value for property 'width'.
验证时出错,HRESULT = '8000000A'
WIN7打补丁后VS2012出现版本不兼容
VS的快捷键
WPF学习记录1:ListView的一个模板
使用Xposed Installer实现Android Hook
原文地址:https://www.cnblogs.com/RuiLei/p/647303.html
最新文章
复习数据库相关知识
android 隐藏输入法键盘
ObjectAnimator ValueAnimator AnimatorSet基础
android Window Leaked异常的解决方法
eclipse 和 android studio 互相导入项目乱码问题(65279)
android studio 代码混淆
张量分解
linux开机启动任务和定时执行任务 crontab
exit
堆排序和快速排序的比较
热门文章
快乐的小蜗牛要开始一年的程序媛之旅啦啦啦!
设置hr标签的粗细
java回调初步学习
android studio 控制台中文乱码
Intent四个重要属性
禁止从别的应用启动Activity
Activity四种启动模式
Android中Parcelable接口用法
ASP.NET MVC view引入命名空间
sqlserver重置标识
Copyright © 2011-2022 走看看