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
>
查看全文
相关阅读:
第三章 AjaxPro框架
第一章 ASP.NET XML与JSON
第二章 ASP.NET Ajax核心对象
第五次作业
第四次作业
第三周笔记
第二周笔记
Java作业
日期顺时,自动跳过节假日
利用java实现excel转pdf文件
原文地址:https://www.cnblogs.com/RuiLei/p/647303.html
最新文章
对软件工程课程的期望
自我介绍
作业
软件测试作业三
软件测试上机实验报告
SE homework2
SPM Homework 1 —A Project From My Personal Life
SE Homework 1 —An Error Impressed Me
软件测试 homework3
软件测试 实验一
热门文章
软件项目管理 homework1
软件测试 homework2
软件测试 homework1
welcome to my cnblog!
软件测试图覆盖
软件测试实验——junit, hamcrest 和 eclemma的安装及使用
软件测试作业二——找出程序中的错误
软件测试第一次作业——描述曾经遇到的bug
初识Django框架——环境搭建前你需要了解的几点
第四章 jQuery Ajax应用
Copyright © 2011-2022 走看看