にLINQを使用してXMLに要素を追加:私はいくつかの要素を追加するために使用したコードのこの部分を持っているXML
string xmlTarget = string.Format(@"<target name='{0}' type='{1}' layout='${{2}}' />",
new object[] { target.Name, target.Type, target.Layout });
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var xmlDoc = XElement.Load(configuration.FilePath);
var nlog = xmlDoc.Elements("nlog");
if (nlog.Count() == 0)
{
return false;
}
xmlDoc.Elements("nlog").First().Elements("targets").First().Add(xmlTarget);
xmlDoc.Save(configuration.FilePath,SaveOptions.DisableFormatting);
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("nlog");
return true;
それはXMLにターゲットを追加することになって、問題は、それがで「<」を置き換えています「<
」と「>」と「>
」が混在しています。
どうすれば修正できますか?
注意は、nlogには注意しないでください。私はlinqtoxmlの問題を懸念しています。
簡単なメモ:コードの特定の部分に注意を払わないようにする簡単な方法があります。 (if(nlog.Count()== 0) 'を使用するよりも' if(!nlog.Any()) 'を使う方が良いです。) –