【发布时间】:2012-08-01 15:54:45
【问题描述】:
我有一个变量失败,它可以有三个值:通过、失败或错误。我希望在序列化期间发生的是基于值创建一个元素。对于通过,我想忽略该元素。对于失败,我想要一个失败元素。对于错误,我想要一个错误元素。如何做到这一点?
在 Test 类中,有一个名为 m_steps 的步骤列表。在 Step 中有一个名为 m_failure 的变量,它保存通过、失败或错误。我希望 m_steps 创建的元素是非元素(通过)、失败或错误。
[XmlRoot("testsuite")]
public class Suite
{
[XmlAttribute("name")]
public string m_name;
[XmlElement("testcase")]
public List<Test> m_tests;
[XmlIgnore]
public string m_timestamp;
public class Test
{
[XmlAttribute("name")]
public string m_name;
[XmlElement("failure")] // want to be ignore, failure or error instead of just failure
public List<Step> m_steps;
[XmlAttribute("assertions")]
public int m_assertions;
}
public class Step
{
[XmlIgnore]
public string m_failure; // holds passed, failure, or error
[XmlTextAttribute]
public string m_message;
[XmlIgnore]
public string m_image;
[XmlAttribute("type")]
public string m_type;
}
正在寻找:
收件人:
<?xml version="1.0" encoding="UTF-8"?>
-<testsuite name=" SimpleCalculationsSuite" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<testcase name="Add 3+4" assertions="1"></testcase>
-<testcase name="Fail 3-4" assertions="1">
<failure type="Text">The control text is not correct. Expected-7 Actual--1</failure>
</testcase>
-<testcase name="Error 3*4" assertions="1">
<error type="Click">The * button was not found</error>
</testsuite>
发件人:
Suite: SimpleCalculationsSuite
Test:Add 3+4
Passed:Text:The control text, 7, is correct.
Test:Fail 3-4
Failed:Text:The control text is not correct. Expected-7 Actual--1
Test:Error 3*4
Error:Click: The * button was not found
【问题讨论】:
-
在我看来,枚举会是更好的方法,不是吗?
-
你的意思是你想要一个Failure类型的变量还是Error类型的变量?
-
只是对您的代码的快速说明 - 您将“m_”用于公共成员。 “m_”通常用于私有成员,有时也用于受保护成员。
-
我并不太担心。变量应该是私有的。但我只是通过命令行运行它以将文本文件转换为 xml。我从不访问变量,只是通过构造函数设置它们。
-
您能否提供您想要的模型/示例 xml 输出。我不完全确定我可以看到与您相同的图片,因为在我看来,将其重命名为 Steps 或 Results,将是最简单的方法,并告诉人们查看或查看自己的实际步骤的 m_failure 字段.
标签: c# xml serialization xml-serialization