【问题标题】:How Do i modify the code to read multiple testcase tags in an asset tag如何修改代码以读取资产标签中的多个测试用例标签
【发布时间】:2016-06-06 18:11:50
【问题描述】:
 public void ConvertToDirectoryTree()
    {
      XmlReader xReader = XmlReader.Create(xmlDoc);

      while (!xReader.EOF)
      {
        if (xReader.Name != "Asset")
        {
          xReader.ReadToFollowing("Asset");
        }

        if (!xReader.EOF)
        {
          XElement asset = (XElement)XElement.ReadFrom(xReader);

            //We check if the asset is a main branch folder
            if (IsMainBranch((string)asset.Attribute("Name") + (string)asset.Attribute("Version")))
            {
              //If the folder exists already then add it inside this folder
              if (Directory.Exists(root + (string)asset.Attribute("Name")))
              {
                XElement testCase = asset.Element("TestCase");
                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
              }
              //Else we need to create the folder and then add it inside this folder
              else
              {
                XElement testCase = asset.Element("TestCase");
                Directory.CreateDirectory(root + (string)asset.Attribute("Name"));
                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
              }
            }
            //If it is not a main branch folder then we need to handle the name differently
            else
            {
              //If the folder exists already then add it inside this folder
              if (Directory.Exists(root + (string)asset.Attribute("Name") + "-" + (string)asset.Attribute("Version")))
              {
                XElement testCase = asset.Element("TestCase");
                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "-" + (string)asset.Attribute("Version") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
              }
              //Else we need to create the folder and then add it inside this folder
              else
              {
                XElement testCase = asset.Element("TestCase");
                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "-" + ((string)asset.Attribute("Version")).Replace(".","_"));
                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "-" + (string)asset.Attribute("Version") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
              }

            }
          }
        }
      }

我有一个函数当前读取以下格式的 XML 文件:

<Properties>
 <Assets>

 <Asset Name="" Version="">
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" />  
  </Asset>

<Asset Name="" Version="">
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" /> 
  <TestCase Name="" Version="" SubVersion="" />  
  </Asset>
</Assets>
</Properties>

目前它会读取每个资产的第一个测试用例,然后跳出并转到下一个资产,如果有人知道如何解决这个问题的话。

【问题讨论】:

  • 添加for循环:foreach(XElement testCase in assets.Elements("TestCase"){}
  • @PhillipsJP。一旦问题得到解答,请不要更改您的问题。如果您有新问题,请提出新问题。

标签: c# xml xmlreader


【解决方案1】:

代码应该是这样的

        public void ConvertToDirectoryTree()
        {
            XmlReader xReader = XmlReader.Create(xmlDoc);

            while (!xReader.EOF)
            {
                if (xReader.Name != "Asset")
                {
                    xReader.ReadToFollowing("Asset");
                }

                if (!xReader.EOF)
                {
                    XElement asset = (XElement)XElement.ReadFrom(xReader);
                    foreach (XElement testCase in asset.Elements("TestCase"))
                    {

                        //We check if the asset is a main branch folder
                        if (IsMainBranch((string)asset.Attribute("Name") + (string)asset.Attribute("Version")))
                        {
                            //If the folder exists already then add it inside this folder
                            if (Directory.Exists(root + (string)asset.Attribute("Name")))
                            {
                               Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
                            }
                            //Else we need to create the folder and then add it inside this folder
                            else
                            {
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name"));
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
                            }
                        }
                        //If it is not a main branch folder then we need to handle the name differently
                        else
                        {
                            //If the folder exists already then add it inside this folder
                            if (Directory.Exists(root + (string)asset.Attribute("Name") + "-" + (string)asset.Attribute("Version")))
                            {
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "-" + (string)asset.Attribute("Version") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
                            }
                            //Else we need to create the folder and then add it inside this folder
                            else
                            {
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "-" + ((string)asset.Attribute("Version")).Replace(".", "_"));
                                Directory.CreateDirectory(root + (string)asset.Attribute("Name") + "-" + (string)asset.Attribute("Version") + "\\" + (string)testCase.Attribute("Version") + (string)testCase.Attribute("SubVersion"));
                            }

                        }
                    }
                }
            }
        }

【讨论】:

  • 是的,工作有点像我有一些包含测试用例的资产标签,但是当我将它提供给我的 Directory.CreateDirectory 时,永远不会创建文件夹是我的逻辑有问题吗或者您如何看待我将如何创建目录树。我这样做是为了稍后收集一份清单。我不确定这是否是处理事情的最佳方式,但我想从此 XML 文件中列出一个列表,并与另一个列表进行比较,在该列表中我将比较 test.case 版本与颠覆相结合的名称以及资产名称+版本的父文件夹
  • 我无法从你的 xml 输入中看出你是如何得到一棵树的。一棵树通常会有嵌套标签;或者为每个目录设置一个根名称。您的 xml 既没有来自您发布的内容。对于嵌套标签,我通常推荐如下递归算法:stackoverflow.com/questions/28976601/…
  • 树我实际上并不是指数据结构树,只是一个目录树,就像我创建文件夹以模仿文件夹结构在我的计算机或存储库中的外观。我基本上这样做是为了将它与另一个目录“树”进行比较,看看我是否有任何重复。所以 TLDR:从我已经拥有的当前目录树结构的列表中删除,这些目录树结构与我的 XML 文件具有相同的名称和父级(路径)。
  • 另外我想知道你是否知道为什么有些资产标签只包含例如一个测试用例标签之间的信息没有被拾取,因为在父文件夹中什么都没有这是资产标签。如果你我的“”之间的颠覆数据是空的,所以它的字面意思是subVersion =“”,那会弄乱数据吗?
  • 通常我会创建一个名为 STATE 的变量(通常会枚举可能的值)并在读取 XML 时进行更新。所以我根据从 XML 和 STATE 变量读取的内容创建了一个 IF(或 switch)语句。
猜你喜欢
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多