0

I've got this xml file:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="MyApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
<startup><supportedRuntime version="v2.0.50727"/></startup>
<applicationSettings>
    <MyApp.Settings>
        ...
        ...
    </XNet.XManager.Properties.Settings>
</applicationSettings>

I need to replace the <startup> node with:

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>

Which is the best way?

4
  • 1
    Well I'd use LINQ to XML to read the whole document, replace the element you're interested, then write it out again. Have you tried that or anything similar? Commented Feb 15, 2016 at 14:23
  • No I prefer to don't use LINQ Commented Feb 15, 2016 at 14:25
  • What have you tried so far? And you should look into LINQ. It can be scary at first, but its incredibly powerful. Commented Feb 15, 2016 at 14:25
  • @Sethi: Why? I suspect your reason won't hold water in the long run. The code using LINQ to XML will be simpler than the equivalent using XmlDocument. Commented Feb 15, 2016 at 14:37

2 Answers 2

2

If you use LINQ to XML (it's an XML API rather than LINQ):

XDocument doc = XDocument.Load("dat.xml");

XElement startup1 = doc.Root.Element("startup");
startup1.Remove();

doc.Root.Add(new XElement("startup", new XAttribute("useLegacyV2RuntimeActivationPolicy", "true"),
                               new XElement("supportedRuntime", new XAttribute("version", "v4.0"),
                               new XAttribute("sku", ".NETFramework"),
                               new XAttribute("Version", "v4.5.2"))));

doc.Save("dat.xml");

Edit - as Jon Skeet suggested the proper way should be to use XElement.ReplaceWith :

XDocument doc = XDocument.Load("dat.xml");

XElement startup1 = doc.Root.Element("startup");           
startup1.ReplaceWith(new XElement("startup", new XAttribute("useLegacyV2RuntimeActivationPolicy", "true"),
                               new XElement("supportedRuntime", new XAttribute("version", "v4.0"),
                               new XAttribute("sku", ".NETFramework"),
                               new XAttribute("Version", "v4.5.2"))));

doc.Save("dat.xml");
Sign up to request clarification or add additional context in comments.

3 Comments

Possibly nicer - use XElement.ReplaceWith, so it stays in the same location.
Oh sorry.. Just doc.Save("dat.xml")... ok.. But: the original version was "<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />" but your code replace the ".NETFramework,Version=.." without the comma: "sku=".NETFramework Version=v4.5.2"
Auto answer ;-) : doc.Root.Add(new XElement("startup", new XAttribute("useLegacyV2RuntimeActivationPolicy", "true"), new XElement("supportedRuntime", new XAttribute("version", "v4.0"), new XAttribute("sku", ".NETFramework,Version=v4.5.2"))));
0

You can use the below code to do the same, where the element is being find and it is replaced with other.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("path to your file");

string strXml = 
  @"<startup useLegacyV2RuntimeActivationPolicy='true'>
    <supportedRuntime version='v4.0' sku='.NETFramework,Version=v4.5.2' />
</startup>";
XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();
xmlDocFragment.InnerXml = strXml;
xmlDoc.SelectSingleNode("startup").AppendChild(xmlDocFragment);

Update: Using LINQ. Working Tested Code

var doc = XDocument.Load(@"path to file");
string input = @"<startup useLegacyV2RuntimeActivationPolicy='true'>
<supportedRuntime version='v4.0' sku='.NETFramework,Version=v4.5.2' />
</startup>";
var replacement = XElement.Parse(input);
var nodeToReplace = doc.Descendants().Elements("startup").FirstOrDefault();
nodeToReplace.ReplaceWith(replacement);
doc.Save(@"path to file");
Console.WriteLine(doc);
Console.Read();

2 Comments

That code won't compile. After fixing it, I'd say it's also significantly less elegant than LINQ to code constructing the elements programmatically.
Nope, that's still not going to compile. It's not a valid verbatim string literal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.