【求助】关于创建XML的问题
XML文件格式:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="Location.aspx">
<system.web>
<authorization>
<allow users="test"/>
</authorization>
</system.web>
</location>
<location path="Customer.aspx">
<system.web>
<authorization>
<deny users="test"/>
</authorization>
</system.web>
</location>
</configuration>
自己编写代码让他自动创建
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("web.config");
XmlNode root1 = xmlDoc.SelectSingleNode("configuration");
XmlElement xe1 = xmlDoc.CreateElement("location");
xe1.SetAttribute("path", "Location.aspx");
XmlElement xe2 = xmlDoc.CreateElement("system.web");
xe2.SetAttribute("xxx", "xxx.aspx");
XmlElement xesub1 = xmlDoc.CreateElement("system.web");
xesub1.InnerText ="";
xe1.AppendChild(xesub1);
root1.AppendChild(xe1);
xmlDoc.Save("web.config");
生成这样的了:
<location path="Location.aspx">
<system.web>
----------问题在这----------
</system.web>
</location>
后面不知道怎么写了。求高手帮忙 @_@我在做一个控制权限的东东。
------解决方案--------------------XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("web.config");
XmlNode root1 = xmlDoc.SelectSingleNode("configuration");
XmlElement xe1 = xmlDoc.CreateElement("location");
xe1.SetAttribute("path", "Location.aspx");
//XmlElement xe2 = xmlDoc.CreateElement("system.web"); //这两行没用上吧?
//xe2.SetAttribute("xxx", "xxx.aspx");
XmlElement xesub1 = xmlDoc.CreateElement("system.web");
//xesub1.InnerText =""; //这行也不知道你想干什么
//---
XmlElement xesub11 = xmlDoc.CreateElement("authorization");
XmlElement xesub111 = xmlDoc.CreateElement("allow");
xesub111.SetAttribute("users", "test");
xesub11.AppendChild(xesub111);
xesub1.AppendChile(xesub11);
//---
xe1.AppendChild(xesub1);
root1.AppendChild(xe1);
xmlDoc.Save("web.config");