我有一個變量失敗,可以有三個值:通過,失敗或錯誤。在序列化過程中我想要發生的是根據值創建一個元素。對於通過,我想忽略這個元素。對於失敗,我想要一個失敗的元素。對於錯誤,我想要一個錯誤元素。如何才能做到這一點?C#xml序列化 - 屬性的多個選擇
在Test類中,有一個名爲m_steps的步驟列表。在步驟中有一個稱爲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
在我看來,一個枚舉會是一個更好的方法,不是嗎? – Icarus 2012-08-01 16:00:07
你的意思是你想要一個類型爲Failure的變量或一個類型爲Error的變量? – SixOThree 2012-08-01 16:07:36
您的代碼只是一個快速點 - 您正在爲公共成員使用「m_」。 「m_」通常用於私人或有時受保護的成員。 – 2012-08-01 16:18:34