我有这样的班级结构:

 public List<EndpointInfo> EndpointInfoList = new List<EndpointInfo> ();


 [Serializable]
    public class EndpointInfo
    {
        public List<PairedEndpoint> PairedEndpoints { get; set; }

        public EndpointInfo ()
        {
            PairedEndpoints         = new List<PairedEndpoint> ();
        }

    }

    public class PairedEndpoint
    {
        public List<int>    ConnectedChannels { get; set; }

        public PairedEndpoint ()
        {
            ConnectedChannels = new List<int>();
        }
    }

我希望得到的xml如下所示
<?xml version="1.0"?>
<ArrayOfEndpointInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <EndpointInfo>
    <PairedEndpoints>
      <PairedEndpoint>
        <ConnectedChannels>
            <ConnectedChannel>1</ConnectedChannel>
            <ConnectedChannel>2</ConnectedChannel>
        </ConnectedChannels>
      </PairedEndpoint>
      <PairedEndpoint>
        <ConnectedChannels>
            <ConnectedChannel>3</ConnectedChannel>
            <ConnectedChannel>4</ConnectedChannel>
        </ConnectedChannels>
      </PairedEndpoint>
    </PairedEndpoints>
  </EndpointInfo>
</ArrayOfEndpointInfo>

但是,我不知道应该如何序列化pairendpoints以在其中创建通道列表。如果有任何帮助,我将不胜感激。

最佳答案

试试这个:

public class ArrayOfEndpointInfo
{
    [XmlElement(ElementName = "EndpointInfo")]
    public EndpointInfo EndPointInfo { get; set; }
}


public class EndpointInfo
{
    [XmlArray(ElementName = "PairedEndpoints")]
    public List<PairedEndpoint> PairedEndpoints { get; set; }

}
public class PairedEndpoint
{
    [XmlArrayItem(ElementName="ConnectedChannel")]
    public List<int> ConnectedChannels { get; set; }
}

然后:
var t = new XmlSerializer(typeof(ArrayOfEndpointInfo));
var result = t.Deserialize(new StreamReader("path"));

07-28 01:24
查看更多