protected void btnCreate_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//定义XMLDocument
XmlDocument xmlDocument = new XmlDocument();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//定义XML文档头文件
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
//增加XML文档头
xmlDocument.AppendChild(xmlDeclaration);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//定义XML的根
XmlElement xmlRoot = xmlDocument.CreateElement("Roots");
//添加XML的根
xmlDocument.AppendChild(xmlRoot);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加根的属性
xmlRoot.SetAttribute("RootAttribute", "Value");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//修改根属性的值
xmlRoot.GetAttributeNode("RootAttribute").Value = "FixValue";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//定义节点
XmlNode xmlElement;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//循环创建节点
for (int i = 0; i < 2; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//创建XML根的节点
xmlElement = xmlDocument.CreateElement("Element");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//XML需要的属性列表
foreach (KeyValuePair<String, String> keyValuePair in NameValueDictionary())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//定义XML根的节点中的属性
XmlAttribute oneAttribute = xmlDocument.CreateAttribute(keyValuePair.Key);
oneAttribute.Value = keyValuePair.Value;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
XmlAttribute secAttribute = xmlDocument.CreateAttribute(keyValuePair.Key);
secAttribute.Value = keyValuePair.Value;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加XML根的节点中的属性
xmlElement.Attributes.Append(oneAttribute);
xmlElement.Attributes.Append(secAttribute);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加XML根的节点
xmlRoot.AppendChild(xmlElement);
}
}
//保存XML文档
xmlDocument.Save(Server.MapPath("OutDocument.XML"));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//XML需要的属性列表
private Dictionary<String, String> NameValueDictionary()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Dictionary<String, String> nameValueDictionary = new Dictionary<String, String>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
nameValueDictionary.Add("AttributeOne", "One");
nameValueDictionary.Add("AttributeSec", "Second");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return nameValueDictionary;
}
生成的XML模块
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
<Roots RootAttribute="FixValue">
<Element AttributeOne="One" AttributeSec="Second" />
<Element AttributeOne="One" AttributeSec="Second" />
</Roots>
posted @
2007-02-11 10:25
RicoRui
阅读(
1835)
评论()
编辑
收藏
举报