C# 将XML反序列化为具有属性的类

C# 将XML反序列化为具有属性的类,c#,xml,xml-serialization,C#,Xml,Xml Serialization,我有一个客户端收到的XML,我需要将它序列化到类结构中。下面是XML结构 <?xml version="1.0" encoding="utf-8"?> <MainData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <listofChannels> <item type="DAY CHANNEL

我有一个客户端收到的XML,我需要将它序列化到类结构中。下面是XML结构

<?xml version="1.0" encoding="utf-8"?>
<MainData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<listofChannels>
   <item type="DAY CHANNEL">
     <channelName>one</channelName>
     <channelPort>11</channelPort>
     <ServerDetail ipaddress="127.0.0.1" port="80"/>
    </item>
   <item type="NIGHT CHANNEL">
     <channelName>one</channelName>
     <channelPort>11</channelPort>
     <ServerDetail ipaddress="127.0.0.2" Port="80"/>
 </item>
</listofChannels>
</MainData>
这是我的课

public class MainData
{
    public List<Channel> listofChannels { get; set; }
}

public class Channel
{
    public string Type;
    [XmlAttribute("channelName")]
    public string Name;
    [XmlAttribute("channelPort")]
    public int Port;
    public ChannelDetail details;
}

public class ChannelDetail
{
    [XmlAttribute("ipaddress")]
    public string IPAddress { get; set; }

    [XmlAttribute("port")]
    public int Port { get; set; }
}
公共类主数据
{
频道的公共列表列表{get;set;}
}
公共类频道
{
公共字符串类型;
[XmlAttribute(“channelName”)]
公共字符串名称;
[XmlAttribute(“channelPort”)]
公共国际港口;
公共渠道详情;
}
公共类频道详细信息
{
[XmlAttribute(“ipaddress”)]
公共字符串IPAddress{get;set;}
[XmlAttribute(“端口”)]
公共int端口{get;set;}
}

应该填充通道类的元素名为“item”。由于它们不匹配,您必须手动指定它(ChannelDetail-ServerDetail也是如此)

还将一些元素映射到属性。我将类定义更改如下,现在似乎工作正常:

public class MainData
{
    public List<Channel> listofChannels { get; set; }
}

[XmlType("item")]
public class Channel
{
    [XmlAttribute("type")]
    public string Type;

    [XmlElement("channelName")]
    public string Name;

    [XmlElement("channelPort")]
    public int Port;

    [XmlElement("ServerDetail")]
    public ChannelDetail details;
}

public class ChannelDetail
{
    [XmlAttribute("ipaddress")]
    public string IPAddress { get; set; }

    [XmlAttribute("port")]
    public int Port { get; set; }
}
公共类主数据
{
频道的公共列表列表{get;set;}
}
[XmlType(“项目”)]
公共类频道
{
[XmlAttribute(“类型”)]
公共字符串类型;
[XmlElement(“通道名称”)]
公共字符串名称;
[XmlElement(“channelPort”)]
公共国际港口;
[XmlElement(“服务器详细信息”)]
公共渠道详情;
}
公共类频道详细信息
{
[XmlAttribute(“ipaddress”)]
公共字符串IPAddress{get;set;}
[XmlAttribute(“端口”)]
公共int端口{get;set;}
}

此外,ServerDetail中的“port”属性在问题示例中的拼写也不同。由于XML区分大小写,您必须确保它们都具有相同的大小写,并使用XmlAttribute[“Port”]或XmlAttribute[“Port”]将其映射到端口变量。

应该填充通道类的元素命名为“item”。由于它们不匹配,您必须手动指定它(ChannelDetail-ServerDetail也是如此)

还将一些元素映射到属性。我将类定义更改如下,现在似乎工作正常:

public class MainData
{
    public List<Channel> listofChannels { get; set; }
}

[XmlType("item")]
public class Channel
{
    [XmlAttribute("type")]
    public string Type;

    [XmlElement("channelName")]
    public string Name;

    [XmlElement("channelPort")]
    public int Port;

    [XmlElement("ServerDetail")]
    public ChannelDetail details;
}

public class ChannelDetail
{
    [XmlAttribute("ipaddress")]
    public string IPAddress { get; set; }

    [XmlAttribute("port")]
    public int Port { get; set; }
}
公共类主数据
{
频道的公共列表列表{get;set;}
}
[XmlType(“项目”)]
公共类频道
{
[XmlAttribute(“类型”)]
公共字符串类型;
[XmlElement(“通道名称”)]
公共字符串名称;
[XmlElement(“channelPort”)]
公共国际港口;
[XmlElement(“服务器详细信息”)]
公共渠道详情;
}
公共类频道详细信息
{
[XmlAttribute(“ipaddress”)]
公共字符串IPAddress{get;set;}
[XmlAttribute(“端口”)]
公共int端口{get;set;}
}

此外,ServerDetail中的“port”属性在问题示例中的拼写也不同。由于XML区分大小写,您必须确保它们都具有相同的大小写,并通过使用XmlAttribute[“Port”]或XmlAttribute[“Port”]将其映射到端口变量。

我建议使用JSON序列化,因为如果存在不安全字符,XML序列化可能会导致问题,除非您计划转义字符串。我建议使用JSON序列化,因为如果存在不安全字符,XML序列化可能会导致问题,除非您计划转义字符串。XML中的端口只是一个输入错误。事实上,它是“端口”,您的解决方案在XML中使用finePort只是一个输入错误。事实上,它是“端口”,您的解决方案运行良好