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>
目前它将阅读每个资产的第一个测试用例,但随后跳出并转到下一个资产,任何人都知道如何解决这个问题。
答案 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"));
}
}
}
}
}
}