2012-08-01 50 views
0

我有一個變量失敗,可以有三個值:通過,失敗或錯誤。在序列化過程中我想要發生的是根據值創建一個元素。對於通過,我想忽略這個元素。對於失敗,我想要一個失敗的元素。對於錯誤,我想要一個錯誤元素。如何才能做到這一點?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 
+0

在我看來,一個枚舉會是一個更好的方法,不是嗎? – Icarus 2012-08-01 16:00:07

+0

你的意思是你想要一個類型爲Failure的變量或一個類型爲Error的變量? – SixOThree 2012-08-01 16:07:36

+0

您的代碼只是一個快速點 - 您正在爲公共成員使用「m_」。 「m_」通常用於私人或有時受保護的成員。 – 2012-08-01 16:18:34

回答

0

我最終修改了測試以包含錯誤列表。通過這個,我能夠在XML文件中出現故障節點和錯誤節點。我也必須修改一些程序以允許這種情況發生。

0

我不會從你的前使用相同的類充足的,我會舉一個例子。

您需要實現public bool ShouldSerialize * 名稱 *()其中Name是要控制序列化的屬性的名稱。

例如,我有這個類:

[Serializable] 
public class SerializeExample 
{ 
    public string ResultType { get; set; } 

    public string Error { get; set; } 

    public string Failure { get; set; } 

    public List<string> Steps { get; set; } 

    public bool ShouldSerializeError() 
    { 
     return "Error".Equals(ResultType); 
    } 

    public bool ShouldSerializeFailure() 
    { 
     return "Failure".Equals(ResultType); 
    } 

    public bool ShouldSerializeSteps() 
    { 
     return ShouldSerializeError() || ShouldSerializeFailure(); 
    } 
} 

如果我有這樣的類的實例:

SerializeExample ex1 = new SerializeExample { ResultType = "Success" }; 

然後,僅此XML序列化:

<SerializeExample> 
    <ResultType>Success</ResultType> 
</SerializeExample> 

如果我創建這個其他類並將其序列化:

SerializeExample ex2 = new SerializeExample { ResultType = "Error", Error = "Invalid data from user", Steps = new List<string>() }; 

然後,生成此XML。

<SerializeExample> 
    <ResultType>Error</ResultType> 
    <Error>Invalid data from user</Error> 
    <Steps /> 
</SerializeExample> 
+0

這並不完全符合我的需求。我有問題,因爲我有兩個班。關於是否創建節點失敗或錯誤的決定是基於需要在Test類Step.failure之外的屬性上做出的決定。 – TIMBERings 2012-08-01 17:22:44

+0

@TIMBERings哦,真的!然後把3個不同的元素,[XmlElement(「失敗」)] [XmlElement(「錯誤」)]和[XmlElement(「忽略」)]和用戶ShouldSerializeWhatever()...根據集合是空的還是有值。這個技巧實際上會起作用。否則,歡迎實現ISerializable的好任務 – 2012-08-01 19:41:45