2013-06-24 128 views
1

我想刪除所有說「SetActiveLocale」並且值爲「en」的交易,除了第一個SetActiveLocale交易。我正在使用XML文檔來存儲整個字符串文件。如何刪除C#中的XML文檔中的重複節點?

<bmtactionlog> 
    <transaction saved="false" seq="1" timestamp="20130621143502322"> 
      <action seq="1" type="SetActiveLocale"> 
       <inputparams> 
        <param seq="1" type="i18nstring"> 
         <value>en</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="true" seq="2" timestamp="20130621143502322"> 
      <action seq="1" type="Modify"> 
       <inputparams> 
        <param seq="1" type="handle"> 
         <mappingpath>shortcut/description</mappingpath> 
         <value>/O/description[0]/O/[Employee expense].[Account]</value> 
        </param> 
        <param seq="2" type="i18nstring"> 
         <value>2:34</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="false" seq="1" timestamp="20130621143502327"> 
      <action seq="3" type="SetActiveLocale"> 
       <inputparams> 
        <param seq="1" type="i18nstring"> 
         <value>en</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="true" seq="4" timestamp="20130621143502327"> 
      <action seq="1" type="Modify"> 
       <inputparams> 
        <param seq="1" type="handle"> 
         <mappingpath>shortcut/description</mappingpath> 
         <value>/O/description[0]/O/[Employee expense].[Employee (by manager)]</value> 
        </param> 
        <param seq="2" type="i18nstring"> 
         <value>2:34</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="false" seq="1" timestamp="20130621143502327"> 
      <action seq="5" type="SetActiveLocale"> 
       <inputparams> 
        <param seq="1" type="i18nstring"> 
         <value>en</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="true" seq="6" timestamp="20130621143502327"> 
      <action seq="1" type="Modify"> 
       <inputparams> 
        <param seq="1" type="handle"> 
         <mappingpath>shortcut/description</mappingpath> 
         <value>/O/description[0]/O/[Employee expense].[Employee (by organization)]</value> 
        </param> 
        <param seq="2" type="i18nstring"> 
         <value>2:34</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="false" seq="1" timestamp="20130621143502327"> 
      <action seq="7" type="SetActiveLocale"> 
       <inputparams> 
        <param seq="1" type="i18nstring"> 
         <value>en</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="true" seq="8" timestamp="20130621143502327"> 
      <action seq="1" type="Modify"> 
       <inputparams> 
        <param seq="1" type="handle"> 
         <mappingpath>shortcut/description</mappingpath> 
         <value>/O/description[0]/O/[Employee expense].[Employee (by position)]</value> 
        </param> 
        <param seq="2" type="i18nstring"> 
         <value>2:34</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="false" seq="1" timestamp="20130621143640438"> 
      <action seq="1" type="SetActiveLocale"> 
       <inputparams> 
        <param seq="1" type="i18nstring"> 
         <value>en</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="true" seq="10" timestamp="20130621143640438"> 
      <action seq="1" type="SetSecurityViewAccess"> 
       <inputparams> 
        <param seq="1" type="handle"> 
         <mappingpath>securityView</mappingpath> 
         <value>[].[securityViews].[GO Data Warehouse (analysis)]</value> 
        </param> 
        <param seq="2" type="integer"> 
         <value>1</value> 
        </param> 
        <param seq="3" type="cclnode"> 
         <value> 
          <![CDATA[<securityObjects/>]]></value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="true" seq="11" timestamp="20130621143640439"> 
      <action seq="1" type="SetSecurityViewAccess"> 
       <inputparams> 
        <param seq="1" type="handle"> 
         <mappingpath>package</mappingpath> 
         <value>[].[packages].[GO Data Warehouse (analysis)]</value> 
        </param> 
        <param seq="2" type="integer"> 
         <value>1</value> 
        </param> 
        <param seq="3" type="cclnode"> 
         <value> 
          <![CDATA[<securityObjects/>]]></value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
     <transaction saved="true" seq="12" timestamp="20130621143640439"> 
      <action seq="1" type="Publish"> 
       <inputparams> 
        <param seq="1" type="handle"> 
         <mappingpath>package</mappingpath> 
         <value>[].[packages].[GO Data Warehouse (analysis)]</value> 
        </param> 
        <param seq="2" type="integer"> 
         <value>2</value> 
        </param> 
        <param seq="3" type="i18nstring"> 
         <value>/content/folder[@name=&apos;Sahil&apos;]</value> 
        </param> 
        <param seq="4" type="i18nstring"> 
         <value>GO Data Warehouse (analysis)</value> 
        </param> 
        <param seq="5" type="integer"> 
         <value>1</value> 
        </param> 
        <param seq="6" type="integer"> 
         <value>1</value> 
        </param> 
        <param seq="7" type="integer"> 
         <value>-1</value> 
        </param> 
       </inputparams> 
       <domchanges/> 
       <result success="t"> 
        <outputparams/> 
       </result> 
      </action> 
     </transaction> 
    </bmtactionlog> 
+0

[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

回答

1

這裏是做你正在尋找一個簡潔的方式:

XmlNodeList nodes = doc.SelectNodes("//bmtactionlog/transaction/action[@type='SetActiveLocale']"); 
XmlNode actionNode = doc.SelectSingleNode("//bmtactionlog"); 
for(int i = 1; i < nodes.Count; i++) 
{ 
    actionNode.RemoveChild(nodes[i]); 
} 

這將讓所有的類型SetActiveLocale的交易節點,然後刪除所有,但第一個節點。

1

在這裏,我寫了一個小函數給你。

這是相當醜陋的,但它確實完成了工作。

 public static void RemoveDuplicates(string filePath) 
    { 
     XmlDocument reader = new XmlDocument(); 
     reader.Load(filePath); 

     bool foundApplicable = false; 
     ArrayList removeNodes = new ArrayList(); 
     foreach(XmlNode node in reader.GetElementsByTagName("transaction")) 
     { 
      if (node.FirstChild != null && node.FirstChild.Attributes["type"].Value == "SetActiveLocale") 
      { 
       if (node.SelectSingleNode("action/inputparams/param") != null && node.SelectSingleNode("action/inputparams/param").InnerText == "en") 
       { 
        if (foundApplicable) 
        { 
         // I have to use a list because foreach breaks if I remove a node while the loop is working 
         removeNodes.Add(node); 
        } 
        else 
         foundApplicable = true; 
       } 
      } 
     } 
     foreach (XmlNode node in removeNodes) 
     { 
      node.ParentNode.RemoveChild(node); 
     } 
     reader.Save(filePath); 
    } 
+0

這似乎是可行的,但它似乎給了我一個出於某種原因錯誤。 –

+0

哪個錯誤?確保導入System.XML,System.IO和System.Collections。 –