XmlDocument的应用---创建Xml模板

None.gif   protected   void  btnCreate_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//定义XMLDocument
InBlock.gif
            XmlDocument xmlDocument = new XmlDocument();
InBlock.gif
InBlock.gif            
//定义XML文档头文件
InBlock.gif
            XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0""utf-8"null);
InBlock.gif            
//增加XML文档头
InBlock.gif
            xmlDocument.AppendChild(xmlDeclaration);
InBlock.gif
InBlock.gif            
//定义XML的根
InBlock.gif
            XmlElement xmlRoot = xmlDocument.CreateElement("Roots");
InBlock.gif            
//添加XML的根
InBlock.gif
            xmlDocument.AppendChild(xmlRoot);
InBlock.gif
InBlock.gif            
//添加根的属性
InBlock.gif
            xmlRoot.SetAttribute("RootAttribute""Value");
InBlock.gif
InBlock.gif            
//修改根属性的值
InBlock.gif
            xmlRoot.GetAttributeNode("RootAttribute").Value = "FixValue";
InBlock.gif
InBlock.gif            
//定义节点
InBlock.gif
            XmlNode xmlElement;
InBlock.gif
InBlock.gif            
//循环创建节点
InBlock.gif
            for (int i = 0; i < 2; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//创建XML根的节点
InBlock.gif
                xmlElement = xmlDocument.CreateElement("Element");
InBlock.gif
InBlock.gif                
//XML需要的属性列表
InBlock.gif
                foreach (KeyValuePair<String, String> keyValuePair in NameValueDictionary())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//定义XML根的节点中的属性
InBlock.gif
                    XmlAttribute oneAttribute = xmlDocument.CreateAttribute(keyValuePair.Key);
InBlock.gif                    oneAttribute.Value 
= keyValuePair.Value;
InBlock.gif
InBlock.gif                    XmlAttribute secAttribute 
= xmlDocument.CreateAttribute(keyValuePair.Key);
InBlock.gif                    secAttribute.Value 
= keyValuePair.Value;
InBlock.gif
InBlock.gif                    
//添加XML根的节点中的属性
InBlock.gif
                    xmlElement.Attributes.Append(oneAttribute);
InBlock.gif                    xmlElement.Attributes.Append(secAttribute);
InBlock.gif
InBlock.gif                    
//添加XML根的节点
InBlock.gif
                    xmlRoot.AppendChild(xmlElement);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
//保存XML文档
InBlock.gif
            xmlDocument.Save(Server.MapPath("OutDocument.XML"));
ExpandedBlockEnd.gif        }

None.gif
None.gif        
// XML需要的属性列表
None.gif
         private  Dictionary < String, String >  NameValueDictionary()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Dictionary
<String, String> nameValueDictionary = new Dictionary<String, String>();
InBlock.gif
InBlock.gif            nameValueDictionary.Add(
"AttributeOne""One");
InBlock.gif            nameValueDictionary.Add(
"AttributeSec""Second");
InBlock.gif
InBlock.gif            
return nameValueDictionary;
ExpandedBlockEnd.gif        }

None.gif 生成的XML模块
None.gif
None.gif
<? xml version="1.0" encoding="utf-8" ?>
None.gif
< Roots  RootAttribute ="FixValue" >
None.gif  
< Element  AttributeOne ="One"  AttributeSec ="Second"   />
None.gif  
< Element  AttributeOne ="One"  AttributeSec ="Second"   />
None.gif
</ Roots >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值