C# c语言中列表的XML序列化/反序列化#

C# c语言中列表的XML序列化/反序列化#,c#,xml,serialization,C#,Xml,Serialization,我有这样的班级结构: public List<EndpointInfo> EndpointInfoList = new List<EndpointInfo> (); [Serializable] public class EndpointInfo { public List<PairedEndpoint> PairedEndpoints { get; set; } public EndpointInfo

我有这样的班级结构:

 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>();
        }
    }
public List EndpointInfoList=new List();
[可序列化]
公共类终结点信息
{
公共列表PairedEndpoints{get;set;}
公共端点信息()
{
PairedEndpoints=新列表();
}
}
公共类PairedEndpoint
{
公共列表ConnectedChannel{get;set;}
公共PairedEndpoint()
{
ConnectedChannels=新列表();
}
}
我希望生成的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>

1.
2.
3.
4.
然而,我真的不知道如何序列化PairedEndpoints以在其中创建通道列表。如果有任何帮助,我将不胜感激。

公共类ProcessXml
{
public void ReadXml()
{
var xml=“”+
@"
1.
2.
3.
4.
";
var serializer=newxmlserializer(typeof(ArrayOfEndpointInfo));
serializer.UnknownNode+=序列化程序\u UnknownNode;
serializer.UnknownatAttribute+=序列化程序\u UnknownatAttribute;
var result=serializer.Deserialize(GenerateStreamFromString(xml))为ArrayOfEndpointInfo;
WriteXml(结果);
}
公共无效WriteXml(ArrayOfEndpointInfo)
{
var writer=XmlWriter.Create(@“D:\temp\myXml.xml”);//在这里,您可以使用不同的重载来获取变量中的xml,例如TextWriter等。
var serializer=newxmlserializer(typeof(ArrayOfEndpointInfo));
序列化器。序列化(编写器,信息);
writer.Close();
}
私有静态void序列化程序_UnknownNode(对象发送方,XmlNodeEventArgs e)
{
WriteLine(“未知节点:{0}\t{1}”,e.Name,e.Text);
}
私有静态void序列化程序_UnknownAttribute(对象发送方,XmlAttributeEventArgs e)
{
WriteLine(“未知属性:{0}\t{1}”、e.Attr.Name、e.Attr.Value);
}
公共流生成器StreamFromString(字符串s)
{
var stream=newmemoryStream();
var writer=新的StreamWriter(流);
作者:写;
writer.Flush();
流位置=0;
回流;
}
}
模型类别包括:

[XmlRoot(ElementName = "ConnectedChannels")]
public class ConnectedChannels
{
    [XmlElement(ElementName = "ConnectedChannel")]
    public List<string> ConnectedChannel { get; set; }
}

[XmlRoot(ElementName = "PairedEndpoint")]
public class PairedEndpoint
{
    [XmlElement(ElementName = "ConnectedChannels")]
    public ConnectedChannels ConnectedChannels { get; set; }
}

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

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

[XmlRoot(ElementName = "ArrayOfEndpointInfo")]
public class ArrayOfEndpointInfo
{
    [XmlElement(ElementName = "EndpointInfo")]
    public EndpointInfo EndpointInfo { get; set; }
}
[XmlRoot(ElementName=“ConnectedChannels”)]
公共类连接通道
{
[XmlElement(ElementName=“ConnectedChannel”)]
公共列表ConnectedChannel{get;set;}
}
[XmlRoot(ElementName=“PairedEndpoint”)]
公共类PairedEndpoint
{
[xmlement(ElementName=“ConnectedChannels”)]
公共ConnectedChannels ConnectedChannels{get;set;}
}
[XmlRoot(ElementName=“PairedEndpoints”)]
公共类PairedEndpoints
{
[xmlement(ElementName=“PairedEndpoint”)]
公共列表PairedEndpoint{get;set;}
}
[XmlRoot(ElementName=“EndpointInfo”)]
公共类终结点信息
{
[xmlement(ElementName=“PairedEndpoints”)]
公共PairedEndpoints PairedEndpoints{get;set;}
}
[XmlRoot(ElementName=“ArrayOfEndpointInfo”)]
公共类ArrayOfEndpointInfo
{
[xmlement(ElementName=“EndpointInfo”)]
公共端点信息端点信息{get;set;}
}
试试这个:

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; }
}
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"));