Avatar of g_johnson
g_johnson
Flag for United States of America asked on

writing to existing xml using Linq

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>

Open in new window

.NET ProgrammingXMLC#

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck