开发者

C# XML adding/copying

With the following XML I am trying to copy and add to another XML but I haven't used the C# XML document objects before. So here is the XML

<config>
   <configXML>
      <Connections>
         <Connection excyptedConnection="encrypted string">
      </Conne开发者_开发百科ctions>
   <configXML>
</config>

I want to be able to copy out the Connection or add new Connection information. I want to be able to use the /config/configXML/Connections/ xpath for adding/copying the values.

Anyone that can help?

Thanks


Try something like this:

var path = "c:\\temp\\myXml.xml";       
XDocument doc = XDocument.Load(path);
var element = doc.XPathSelectElement("config/configXML/Connections/Connection");
element.Attribute("encryptedConnection").Value = "Whatever";    
doc.Save(path);


While you can use nifty constructs like Linq for Xml etc., if all you need is to change the value of one (or few) node, just use the XmlDocument class.
It exposes your XML's DOM and provides a simple set of functions to get and manipulate values.

You can either load your XML as a string, or from a file. And there are plenty of samples floating around.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜