【发布时间】: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。一旦问题得到解答,请不要更改您的问题。如果您有新问题,请提出新问题。