我如何修改代码以读取资产标签中的多个测试用例标签 [英] How Do i modify the code to read multiple testcase tags in an asset tag

查看:25
本文介绍了我如何修改代码以读取资产标签中的多个测试用例标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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 文件:

I have a function that currently reads an XML file of the following format:

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

目前它将读取每个资产的第一个测试用例,然后跳出并转到下一个资产,任何人都知道如何解决这个问题.

Currently it will read the first test case of every asset but then jump out and go to the next asset would anyone know how to fix this.

推荐答案

代码应该是这样的

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

                        }
                    }
                }
            }
        }

这篇关于我如何修改代码以读取资产标签中的多个测试用例标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆