【问题标题】:C# xml serialization - multiple choices for a propertyC# xml 序列化 - 属性的多种选择
【发布时间】: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


【解决方案1】:

我不会使用您示例中的相同类,我将举一个我自己的小例子。

您需要实现方法 public bool ShouldSerialize*Name*() 其中 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>

【讨论】:

  • 这并不能完全满足我的需要。我有问题,因为我有两节课。是否创建节点故障或错误的决定取决于需要对 Test 类之外的属性 Step.failure 做出的决定。
  • @TIMBERings 天哪,真的!然后放置 3 个不同的元素, [XmlElement("failure")][XmlElement("error")] 和 [XmlElement("ignore")] 和用户 ShouldSerializeWhatever() ... 基于集合是否为空或有值.这个技巧实际上会起作用。否则,欢迎执行 ISerializable
【解决方案2】:

我最终修改了 Test 以包含错误列表。有了这个,我能够在 XML 文件中同时出现故障和错误节点。我还必须修改一些程序以允许这种情况发生。

【讨论】:

    【解决方案3】:

    【讨论】:

    • 呃。我曾经尝试过手动实现。它是一头野兽,绝不是我推荐的方式。不过它会起作用。了解我,我可能既做错了又过度设计了这件事,但它仍然是一头野兽。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多