XMLSerializer更改元素的名称

时间:2014-07-17 12:01:52

标签: c# xml serialization

我正在尝试将对象序列化以导入另一个软件,问题是,要导入的XML中的元素包含“:”(p.e。:< ftr:filter>)。
我声明类使用[XmlAttribute("ftr:filter")][XMLElement(ftr:differentFilter")]覆盖这些名称,但序列化程序产品使用不同的节点。我敢打赌它与编码有关,但我无法改变结果(以为我改变了编码) 课程示例:

public class ListPrijemkaRequest
{
    [XmlAttribute("version")]
    public string Version { get; set; }
    [XmlAttribute("prijemkaVersion")]
    public string PrijemkaVersion { get; set; }
    [XmlElement("lst:requestPrijemka")]
    public List<RequestPrijemka> Requests { get; set; }
}

public class RequestPrijemka
{
    [XmlElement("ftr:filter")]
    public RequestDateFilter Filter { get; set; }
}

期望的输出:

< lst:listPrijemkaRequest version="2.0" prijemkaVersion="2.0">
    < lst:requestPrijemka>
        < ftr:filter>
            < ftr:dateFrom>2013-01-10</ftr:dateFrom>
            < ftr:dateTill>2013-03-30</ftr:dateTill>
        < /ftr:filter>
    < /lst:requestPrijemka>
< /lst:listPrijemkaRequest>

获得输出:

< lst_x003A_listPrijemkaRequest version="2.0" prijemkaVersion="2.0">
    < lst_x003A_requestPrijemka>
        < ftr_x003A_filter>
            < ftr_x003A_dateFrom>2013-01-10</ftr_x003A_dateFrom>
            < ftr_x003A_dateTill>2013-03-30</ftr_x003A_dateTill>
        < /ftr_x003A_filter>
    < /lst_x003A_requestPrijemka>
< /lst_x003A_listPrijemkaRequest>

2 个答案:

答案 0 :(得分:3)

如果那些&#34;标签&#34; ftr / lst是命名空间,不需要&#34;硬编码&#34;它们,您可以设置序列化程序以使用这些名称空间。

http://msdn.microsoft.com/en-us/library/ms163161%28v=vs.110%29.aspx

示例(取自XML Serialization and namespace prefixes

[XmlRoot("Node", Namespace="http://your.companies.namespace")]
public class ListPrijemkaRequest {
    [XmlElement("requestPrijemka")]
    public List<RequestPrijemka> Requests { get; set; }
}

static class Program
{
    static void Main()
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("lst", "http://your.companies.namespace");
        XmlSerializer xser = new XmlSerializer(typeof(ListPrijemkaRequest));
        xser.Serialize(Console.Out, new ListPrijemkaRequest(), ns);
    }
}

如果没有,我认为可以使用&#34;默认&#34;序列化。

其他选择:

但正如我在评论中所说,不建议首先在节点名称中使用:

答案 1 :(得分:2)

[XmlRoot("listPrijemkaRequest", Namespace = "http://your.companies.namespace/lst")]
public class ListPrijemkaRequest {
    [XmlAttribute("version")]
    public string Version { get; set; }
    [XmlAttribute("prijemkaVersion")]
    public string PrijemkaVersion { get; set; }
    [XmlElement("requestPrijemka")]
    public List<RequestPrijemka> Requests { get; set; }
}

public class RequestDateFilter
{
    [XmlElement(ElementName = "dateFrom")]
    public DateTime DateFrom { get; set; }

    [XmlElement(ElementName = "dateTill")]
    public DateTime DateTill { get; set; }
}

public class RequestPrijemka {
    [XmlElement("filter", Namespace = "http://your.companies.namespace/ftr")]
    public RequestDateFilter Filter { get; set; }
}

static class Program {
    static void Main() {
        var ns = new XmlSerializerNamespaces();
        ns.Add("lst", "http://your.companies.namespace/lst");
        ns.Add("ftr", "http://your.companies.namespace/ftr");
        var xser = new XmlSerializer(typeof(ListPrijemkaRequest));

        var obj = new ListPrijemkaRequest
            {
                Version = "2.0",
                PrijemkaVersion = "2.0",
                Requests = new List<RequestPrijemka>
                    {
                        new RequestPrijemka
                            {
                                Filter = new RequestDateFilter {DateFrom = DateTime.Now, DateTill = DateTime.Now}
                            }
                    }
            };

        xser.Serialize(Console.Out, obj, ns);
        Console.ReadLine();
    }
}

制作这个xml:

<?xml version="1.0" encoding="cp866"?>
<lst:listPrijemkaRequest xmlns:ftr="http://your.companies.namespace/ftr" version="2.0" prijemkaVersion="2.0" xmlns:lst="http://your.companies.namespace/lst">
  <lst:requestPrijemka>
    <ftr:filter>
      <ftr:dateFrom>2014-07-17T16:17:47.0601039+03:00</ftr:dateFrom>
      <ftr:dateTill>2014-07-17T16:17:47.061104+03:00</ftr:dateTill>
    </ftr:filter>
  </lst:requestPrijemka>
</lst:listPrijemkaRequest>

看起来与你需要的相似。