troubleshooting Question

writing to existing xml using Linq

Avatar of g_johnson
g_johnsonFlag for United States of America asked on
XML.NET ProgrammingC#
1 Comment1 Solution304 ViewsLast Modified:
Given the attached xml file, how do I write a new node to the xml file using linq to xml?  I know how to create an xml document from c# code, but not how to amend it.

Two possible examples:
Example 1:
Suppose I want to add a new setting that will end up looking like this:

- <Setting>
  <Heading>Parameters</Heading>
  <Instance>1</Instance>
- <Specifics>
  <SettingName>base folder</SettingName>
  <SettingValue>c:\clients</SettingValue>
  </Specifics>
- <Specifics>
  <SettingName>archive folder</SettingName>
  <SettingValue>c:\archives</SettingValue>
  </Specifics>
- <Specifics>
  <SettingName>defect folder</SettingName>
  <SettingValue>c:\defects</SettingValue>
  </Specifics>
  </Setting>


or Example 2, I just want to add instance 3 of the connection settings

Thanks!

<?xml version="1.0" encoding="utf-8" ?> 
- <CustomSettings>
- <Setting>
  <Heading>Connection</Heading> 
  <Instance>1</Instance> 
- <Specifics>
  <SettingName>db</SettingName> 
  <SettingValue>data1</SettingValue> 
  </Specifics>
- <Specifics>
  <SettingName>server</SettingName> 
  <SettingValue>server1</SettingValue> 
  </Specifics>
  </Setting>
- <Setting>
  <Heading>Connection</Heading> 
  <Instance>2</Instance> 
- <Specifics>
  <SettingName>db</SettingName> 
  <SettingValue>data2</SettingValue> 
  </Specifics>
- <Specifics>
  <SettingName>server</SettingName> 
  <SettingValue>server1</SettingValue> 
  </Specifics>
  </Setting>
  </CustomSettings>
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros