日期:2014-05-20  浏览次数:20782 次

Linq to xml问题,代码没问题,但是却又错,求大神!!
本来我想用linq to xml 来修改asp.net的Web.config文件,但却出现了问题。
这是代码:

     string filename = Text1.Value;
     string fpath = "~/file/video" + filename;
                string xmlFilePath = Server.MapPath(@"../Web.config");
                XElement xe = XElement.Load(xmlFilePath);
                XElement a = new XElement("add",
                    new XAttribute("name", filename),
               new XElement("uploadStreamProvider",
                   new XAttribute("type", "File"),
                   new XAttribute("location", fpath),
                   new XAttribute("existingAction", "Overwrite")
                   )
              );
                xe.Elements("uploadProfiles").First().Add(a);
                xe.Save(xmlFilePath);

这是Web.config文件代码:

 <uploadProfiles>
      <add name="aaa">
        <uploadStreamProvider type="File" location="~/file/video" existingAction="Overwrite" />
      </add>
    </uploadProfiles> 

意思是我想在uploadProfile节点里面增加add节点以及里面的内容,本来这个代码在我自己创建的一个xml文件里面测试时没有问题的,但是一旦换成了Web.config文件,就出现了“序列不包含任何元素”的错误,是在“ xe.Elements("uploadProfiles").First().Add(a);”这一行,我不知道哪里问题,所以请大家帮帮忙。。
------解决方案--------------------
得从configuration开始一级一级的下去啊