如何修改代码以读取资产标签中的多个测试用例标签

时间:2016-06-06 18:11:51

标签: c# xml xmlreader

 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>

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

1 个答案:

答案 0 :(得分: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"));
                            }

                        }
                    }
                }
            }
        }