C#XML序列化 - 更改数组项名称

时间:2014-07-23 07:20:27

标签: c# .net xml xml-serialization

我需要将具有List属性的对象序列化为XML以获取这样的XML代码(我知道它不是有效的XML,但我的第三方应用程序需要这种格式):

<Filters>
    <Criteria_0 Parameter="STATUS"
        Operator="EQUAL"
        Value="STARTED" />
    <Criteria_1 Parameter="STATUS" 
        Operator="EQUAL"
        Value="COMPLETED" />
</Filters>

我写了这样的代码:

public class JobStatusListTask
{
    public JobListSettings ListSettings;

    public List<JobFilterCriteria> Filters;

    public JobStatusListTask()
    {
        Filters = new List<JobFilterCriteria>();
        Filters.Add(new JobFilterCriteria("STATUS", CriteriaOperator.Equal, "ERROR"));
    }


    public JobStatusListTask(JobListSettings settings) : this()
    {
        ListSettings = settings;
    }
}

public class JobFilterCriteria : IXmlSerializable
{
    public static int Count = 0;

    public string Parameter;

    public CriteriaOperator Operator;

    public string Value;

    private JobFilterCriteria()
    {
        Parameter = string.Empty;
        Value = string.Empty;
    }

    public JobFilterCriteria(string parameter, CriteriaOperator criteriaOperator, string value)
    {
        Parameter = parameter;
        Operator = criteriaOperator;
        Value = value;
    }

    XmlSchema IXmlSerializable.GetSchema()
    {
        return null;
    }

    void IXmlSerializable.ReadXml(XmlReader reader)
    {
        throw new NotImplementedException();
    }

    void IXmlSerializable.WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement(string.Format("Criteria_{0}", Count++));
        writer.WriteAttributeString("Parameter", Parameter);
        writer.WriteAttributeString("Operator", Operator.ToString());
        writer.WriteAttributeString("Value", Value);
    }
}

它几乎完美无缺。 Serializer返回带有不必要的JobFilterCriteria元素的XML代码。

<?xml version="1.0" encoding="utf-8"?>
<Filters>
    <JobFilterCriteria>
        <Criteria_0 
            Parameter="STATUS" Operator="Equal" Value="ERROR" />
    </JobFilterCriteria>
    <JobFilterCriteria>
        <Criteria_1 
            Parameter="STATUS" Operator="Equal" Value="STARTED" />
    </JobFilterCriteria>
</Filters>

我需要更改什么才能从XML代码中删除JobFilterCriteria?

2 个答案:

答案 0 :(得分:0)

我认为,这个代码块将解决您的问题,您需要序列化JobStatusListTask类。

public class JobStatusListTask : IXmlSerializable
{
    public JobListSettings ListSettings;

    public List<JobFilterCriteria> Filters;

    public JobStatusListTask()
    {
        Filters = new List<JobFilterCriteria>();
        Filters.Add(new JobFilterCriteria("STATUS", CriteriaOperator.Equal, "ERROR"));
    }


    public JobStatusListTask(JobListSettings settings) : this()
    {
        ListSettings = settings;
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement("Filters");
        foreach(var item in Filters)
        {
            writer.WriteStartElement("Criteria", string.Format("Criteria_{0}", Count++));
            writer.WriteAttributeString("Parameter", Parameter);
            writer.WriteAttributeString("Operator", Operator.ToString());
            writer.WriteAttributeString("Value", Value);
        }
        writer.WriteEndElement();
    }

}

答案 1 :(得分:0)

我无法对答案发表评论,但我不知道上面是如何编译的,看起来命名空间是偶然设置的,我相信你已经解决了,但对后人来说,有一些变化我认为需要使其按预期工作:

public class JobStatusListTask : IXmlSerializable
{
    public JobListSettings ListSettings;

    public List<JobFilterCriteria> Filters;

    public JobStatusListTask()
    {
        Filters = new List<JobFilterCriteria>();
        Filters.Add(new JobFilterCriteria("STATUS", CriteriaOperator.Equal, "ERROR"));
    }


    public JobStatusListTask(JobListSettings settings) : this()
    {
        ListSettings = settings;
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement("Filters");
        for(int i = 0; i < Filters.Count; i++)
        {
            writer.WriteStartElement(string.Format("Criteria_{0}", i));
            writer.WriteAttributeString("Parameter", Parameter);
            writer.WriteAttributeString("Operator", Operator.ToString());
            writer.WriteAttributeString("Value", Value);
        }
        writer.WriteEndElement();
    }

}