将字符串附加到xml文件 [英] Append string to xml file

查看:74
本文介绍了将字符串附加到xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有一个XML:



Hello. I have an XML:

<connectionStrings>
  <add name="networkingEntities" connectionString="bla"/>
  <add name="networkingEntitiesAdmin" connectionString="blabla"/>
  <add name="FtpConnection" connectionString="blabla" />
</connectionStrings>





我想在开头和结尾添加配置和/配置



然后在ano能够移除那些的方法2.



能帮忙吗?



我没有'从未使用xml ...

谢谢



试过这个但是......我得到并且错误:





I want to add at the begining and at the end "configuration" and "/configuration"

and then in another method to be able to remove those 2.

Can you please help?

I haven't worked with xml never...
Thank you

Tried this but...I get and error :

XmlDocument doc = new XmlDocument();
          doc.Load(filename);
          XmlElement fileElement = doc.CreateElement("configuration");
          XmlElement rootElement = (XmlElement)doc.SelectSingleNode("/Root");
          rootElement.AppendChild(fileElement);

          doc.Save(filename);

推荐答案

根据我的理解,你不能在根元素之外使用XmlDocument在XML文件中添加任何东西,而且是< ;的ConnectionStrings取代。其他方法是使用文件处理。以下是样本

As per my understanding you can not add anything in XML file using XmlDocument outside the root element and which is <connectionStrings>. Other way to do that is to use File handling. Below is sample
string xml = File.ReadAllText(filename);
      xml = "<configuration>" + xml + "</configuration>";
      File.WriteAllText(filename, xml);


这篇关于将字符串附加到xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆