如果选择了节点重命名,请重命名节点名称,然后在 xml 中跳过 self


<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="" />

我尝试通过使用以下函数更改其属性值来实现节点名称重命名,但它也在检查自身。

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));
}

不使用 Linq,因为您似乎有一个 XmlDocument 作为您的 xml 容器,我建议使用该实例上可用的方法SelectSingleNode并允许执行 XPath 搜索。您的代码如下所示:

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;
}

最新更新