简体   繁体   English

C# 用新的属性和子节点替换 XML 节点

[英]C# replace XML node with a new attribute and subnode

I've got this xml file:我有这个 xml 文件:

<?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>节点替换为:

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

Which is the best way?哪种方法最好?

If you use LINQ to XML (it's an XML API rather than LINQ):如果您使用 LINQ to XML(它是一个 XML API 而不是 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 :编辑 - 正如 Jon Skeet 建议的正确方法应该是使用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");

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 .更新:使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM