LINQ to XML DOM basic删除和恢复

时间:2016-02-26 08:50:24

标签: c# xml linq

我正在练习这些方法(作为初学者)。

我要切割一个元素(及其所有后代)。

我要粘贴它,以便最后一个应该与原始版本相同。

但是,我有一个问题。

这是我的代码;

var fCONT = XDocument.Parse(
@"<?xml version=""1.0""?>
    <Project xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" Guid=""85e6239d-32f0-4511-a5ac-30790963d4d0"" SettingsBundleGuid=""8d79a512-7582-43fe-8722-0af15553237c"" Version=""4.0.0.0"" Owner=""\Park"" ProjectTemplateGuid=""10004f3b-4acf-4425-a3e3-38d3b3514276"" ReferenceProjectGuid=""00000000-0000-0000-0000-000000000000"">
        <SettingsBundles>
        <SettingsBundle Guid=""8d79a512-7582-43fe-8722-0af15553237c"">
            <SettingsBundle>
            <SettingsGroup Id=""QAVerificationSettings"">
                <Setting Id=""RegExRules0"">
                   <RegExRule xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/Sdl.Verification.QAChecker.RegEx""><Description>The First One</Description><IgnoreCase>false</IgnoreCase><RegExSource></RegExSource><RegExTarget>[0-9]{1,}\s%</RegExTarget><RuleCondition>TargetOnly</RuleCondition></RegExRule></Setting>
            </SettingsGroup>
            </SettingsBundle>
        </SettingsBundle>
        </SettingsBundles>
    </Project>");

XNamespace dNSP = "http://schemas.datacontract.org/2004/07/Sdl.Verification.QAChecker.RegEx";
string aELMT = "RegExRule";

List<XElement> eLIST = new List<XElement> { };
foreach( var x in fCONT.Descendants(dNSP + aELMT))           
    eLIST.Add(x);

fCONT.Descendants().Where( x => Regex.IsMatch((string) x.Attribute("Id") ?? "", @"RegExRules\d+")).Remove();

fCONT.Root.Add(                                         
new XElement("Project",
    new XElement("SettingsBundles",
        new XElement("SettingsBundle",
            new XElement("SettingsGroup",
                new XElement("Setting", new XAttribute("Id", "RegExRules0"),
                    eLIST.Select(a => a)
))))));

var rFILE = Directory.GetCurrentDirectory() + @"\_Texting.xml";
fCONT.Save(rFILE);

它向我展示了以下内容(我不希望这个结果,我想要与原版相同的#34; fCONT&#34;)

  <?xml version="1.0" encoding="utf-8"?>
  <Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Guid="85e6239d-32f0-4511-a5ac-30790963d4d0" SettingsBundleGuid="8d79a512-7582-43fe-8722-0af15553237c" Version="4.0.0.0" Owner="\Park" ProjectTemplateGuid="10004f3b-4acf-4425-a3e3-38d3b3514276" ReferenceProjectGuid="00000000-0000-0000-0000-000000000000">
    <SettingsBundles>
    <SettingsBundle Guid="8d79a512-7582-43fe-8722-0af15553237c">
      <SettingsBundle>
        <SettingsGroup Id="QAVerificationSettings" />
      </SettingsBundle>
    </SettingsBundle>
    </SettingsBundles>
    <Project>
    <SettingsBundles>
      <SettingsBundle>
        <SettingsGroup>
          <Setting Id="RegExRules0">
            <RegExRule xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Sdl.Verification.QAChecker.RegEx">
              <Description>The First One</Description>
              <IgnoreCase>false</IgnoreCase>
              <RegExSource></RegExSource>
              <RegExTarget>[0-9]{1,}\s%</RegExTarget>
              <RuleCondition>TargetOnly</RuleCondition>
            </RegExRule>
          </Setting>
        </SettingsGroup>
      </SettingsBundle>
    </SettingsBundles>
    </Project>
  </Project>

请提供好的提示。 (我怎样才能使用&#34;祖先&#34;轻松?)

谢谢!

0 个答案:

没有答案