首页 新闻 搜索 专区 学院

修改配置文件的问题

0
悬赏园豆:100 [已解决问题] 解决于 2008-08-05 15:21
<P>1:我在app.config文件里自定义了一个section</P> <P>&lt;section name="Notify" type="Roboth.NotifySection,Roboth"/&gt;</P> <P>&lt;Notify&gt;</P> <P>&nbsp;&nbsp;&nbsp; &lt;OrderStateTypes&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/OrderStateTypes&gt;</P> <P>&lt;/Notify&gt;</P> <P>2:我想通过程序向OrderStateTypes添加&lt;add id="1" status="2" ispaid="1" isdeleted="0" statusnotify="payment at full"&gt;&lt;/add&gt;</P> <P>3:code</P> <P>Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NotifySection section = (NotifySection)config.GetSection("Notify");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //string xmlfile = section.MsgTypes["OrderStateChanged"].Parameters["OrderStateTypeXmlPath"].ParamValue.ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OrderStateTypeElement ost = new OrderStateTypeElement();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ost.IsDeleted = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ost.IsPaid = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ost.Status = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ost.Name = "1";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ost.StatusNotify = "jj";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; section.OrderStateTypes.Add(ost);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; config.Save();</P> <P>我该如何实现 呢?</P> <P>请大家指教</P>
roboth的主页 roboth | 初学一级 | 园豆:28
提问于:2008-08-05 13:21
< >
分享
最佳答案
0
有点搞不懂,你贴出的代码已经可以实现了你要的功能了,只需要: 1.OrderStateTypeElement继承于ConfigurationElement,对每一个属性设置相关的特性如ConfigurationProperty等。如: [ConfigurationProperty("status", IsRequired = true)] public string Status { get { return (string)this["status"]; } set { this["status"] = value; } } 2.NotifySection继承于ConfigurationSection,同样设置相关特性 3.定义一个OrderStateType集合类,在NotifySection中会拥有一个该集合的属性: [ConfigurationProperty("OrderStateTypes", IsDefaultCollection = false)] public OrderStateTypeCollection OrderStateTypes { get { OrderStateTypeCollection statetypes (OrderStateTypeCollection )base["OrderStateTypes"]; return statetypes; } }
TerryLee | 老鸟四级 |园豆:3300 | 2008-08-05 15:16
其他回答(2)
0
XmlDocument doc = new XmlDocument(); doc.Load(configFilePath); XmlNode sectionNode = doc.SelectSingleNode("Notify/OrderStateTypes"); XmlNode newNode = doc.CreateElement("add"); XmlAttribute attId = doc.CreateAttribute("id"); attId.Value = youId; newNode.Attributes.Append(attId); ...append other attributes sectionNode.AppendChild(newNode); doc.Save(configFilePath);
玉开 | 园豆:8822 (大侠五级) | 2008-08-05 14:33
0
新建一个XMLFile.xml文件 <?xml version="1.0" encoding="utf-8"?> <Notify> <OrderStateTypes> </OrderStateTypes> </Notify> 代码如下; 在添加的事件里代码如下: XmlDocument xmlDoc = new XmlDocument(); string path = @"D:\\AllDemo\\XML20080614\\App_Data\\XMLFile.xml"; xmlDoc.Load(path); XmlDocumentFragment docFrg = xmlDoc.CreateDocumentFragment(); docFrg.InnerXml = "<add id=\"1\" status=\"2\" ispaid=\"1\" isdeleted=\"0\" statusnotify=\"payment at full\"></add>"; XmlNode nodeObj = xmlDoc["Notify"].SelectSingleNode("OrderStateTypes"); nodeObj.InsertAfter(docFrg, nodeObj.LastChild); xmlDoc.Save(path); 结果是: <?xml version="1.0" encoding="utf-8"?> <Notify> <OrderStateTypes> <add id="1" status="2" ispaid="1" isdeleted="0" statusnotify="payment at full"> </add> </OrderStateTypes> </Notify> 你可以根据这个,修改一下再满足你当前的需求吧!
金鱼 | 园豆:1090 (小虾三级) | 2008-08-05 15:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册