2017-08-11 68 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; 
}