2017-08-11 73 views
0
<TestCase name="TestCase1" UID="1" State="Checked" DataSourceId="1" order="1">  
    </TestCase> 
    <TestCase name="TestCase2" order="2" UID="7c914deb-8f44-4f00-90db-2f36052611c5" State="Checked" DataSourceId="" /> 
<TestCase name="TestCase3" order="3" UID="7c914deb-8f44-4f00-90db-2f36052611c6" State="Checked" DataSourceId="" /> 

我試圖執行節點名稱與下面的功能改變其屬性值重命名,但它檢查自己了。重命名節點名稱選擇的節點重命名,然後跳過自己的XML

private bool RenameTestCase(string oldValue, string newValue, string selectedNodeUID) 
{ 
    bool IsSuccess = false; 
    XmlNodeList nodeListToUpdate = xmlDocument.GetElementsByTagName("TestCase"); 
    foreach (XmlNode node in nodeListToUpdate) 
    { 
     if (node.Attributes[CommonDef.NameTag] != null && 
      node.Attributes[CommonDef.ATTRIBUTE_UID] != null && 
      node.Attributes[CommonDef.ATTRIBUTE_UID].Value != selectedNodeUID && 
      node.Attributes[CommonDef.NameTag].Value == newValue) 
     { 
      MessageBox.Show(node.Attributes[CommonDef.NameTag].Value + " is already exists."); 
      IsSuccess = false; 
     } 
     else 
     { 
      node.Attributes[CommonDef.NameTag].Value = newValue; 
      IsSuccess = true; 
     } 
    } 
    xmlDocument.Save(Path.Combine(l_csConfigFolderPath, CommonDef.TESTSUITE_DATA)); 

} 

回答

1

沒有你似乎有一個XmlDocument爲你的XML容器我會建議使用方法SelectSingleNode上可用該實例並允許做的XPath搜索使用LINQ。您的代碼如下所示:

private bool RenameTestCase(string oldValue, string newValue, string selectedNodeUID) 
{ 
    if (selectedNodeUID == null) throw new ArgumentException("selectedNodeUID", "is null"); 
    if (newValue == null) throw new ArgumentException("newValue", "is null"); 

    bool IsSuccess = false; 
    // check with Xpath 
    // if the any nodes named TestCase : //TestCase[] 
    // where its UID attribute isn't equal: not(@UID='{0}') 
    // and the name attribute equals our newvalue: @name='{1}' 
    var nodeExist = xmlDocument.SelectSingleNode(
     String.Format("//TestCase[not(@UID='{0}') and @name='{1}']", 
      selectedNodeUID, 
      newValue)); 
    if (nodeExist != null) 
    { 
     MessageBox.Show(newValue + " is already exists."); 
     IsSuccess = false; 
    } 
    else 
    { 
     // find the node to update 
     // any TestCase node: //TestCase[] 
     // where the UID attribute equals the selectedUid: @UID='{0}' 
     var node = xmlDocument.SelectSingleNode(
      String.Format("//TestCase[@UID='{0}']", selectedNodeUID)); 
     if (node == null) 
     { 
      // error 
      MessageBox.Show(selectedNodeUID + " UID not found"); 
      IsSuccess = false; 
     } 
     else 
     { 
      // set the new value 
      node.Attributes[CommonDef.NameTag].Value = newValue; 
      IsSuccess = true; 
     } 
    } 

    xmlDocument.Save(Path.Combine(l_csConfigFolderPath, CommonDef.TESTSUITE_DATA)); 

    // don't forget to return something 
    return IsSuccess; 
} 

請注意,我添加了一個return語句,以便您的IsSuccess值返回給調用者。我不確定是否有意但參數oldValue從未使用。考慮刪除它或添加它作爲額外的檢查。我把它作爲讀者的練習。

如果你打開使用的XDocument代替,這可能是您的解決方案:

private bool RenameTestCaseXdoc(string oldValue, string newValue, string selectedNodeUID) 
{ 
    if (selectedNodeUID == null) throw new ArgumentException("selectedNodeUID", "is null"); 
    if (newValue == null) throw new ArgumentException("newValue", "is null"); 

    var xdoc = XDocument.Load(Path.Combine(l_csConfigFolderPath, CommonDef.TESTSUITE_DATA)); 

    var exist = xdoc.Descendants("TestCase") 
     .Where(elem => elem.Attribute("UID").Value != selectedNodeUID 
        && elem.Attribute("name").Value == newValue) 
     .Any(); 
    if (exist) 
    { 
     MessageBox.Show(newValue + " is already exists."); 
     return false; 
    } 
    else 
    { 
     var element = xdoc.Descendants("TestCase") 
        .Where(elem => elem.Attribute("UID").Value == selectedNodeUID) 
        .SingleOrDefault(); 
     if (element == null) 
     { 
      MessageBox.Show(selectedNodeUID + " not found."); 
      return false; 
     } 
     else 
     { 
      element.Attribute("name").Value = newValue; 
     } 
    } 
    xdoc.Save(Path.Combine(l_csConfigFolderPath, CommonDef.TESTSUITE_DATA)); 
    return true; 
}