如果选定的节点重命名,则重命名节点名称,然后在xml中跳过self

时间:2017-08-11 06:40:54

标签: c# xml linq

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

}

1 个答案:

答案 0 :(得分:1)

如果您没有使用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;
}
相关问题