C# 自定义数据源上的XML序列化

C# 自定义数据源上的XML序列化,c#,.net,xml,xml-serialization,C#,.net,Xml,Xml Serialization,我有一个XML文件: <Hand cards="C5,SQ,DQ,H8,C9,H7,S9,D5,DA,CJ,S6,HK,D4"> </Hand> 我定义了一个类 [Serializable()] [XmlRoot("Hand")] public class Hand { [XmlAttribute("cards")] public List<string> Cards{get;set;} } [Serializable()] [XmlR

我有一个XML文件:

<Hand cards="C5,SQ,DQ,H8,C9,H7,S9,D5,DA,CJ,S6,HK,D4">
</Hand>

我定义了一个类

[Serializable()]
[XmlRoot("Hand")]
public class Hand
{
    [XmlAttribute("cards")]
    public List<string> Cards{get;set;}
}
[Serializable()]
[XmlRoot(“手”)]
公务生
{
[XmlAttribute(“卡片”)]
公共列表卡{get;set;}
}
在这种情况下,如何将XML反序列化为对象?手部物体结果必须有卡片={C5、SQ、DQ、H8、C9、H7、S9、D5、DA、CJ、S6、HK、D4}

你不能

您可以做的是创建一个属性,该属性将在其getter/setter中执行此转换

[XmlIgnore]
public List<string> CardList { get; private set; }

[XmlAttribute("cards")]
public string Cards {
   get { return String.Join(",", CardList); }
   set { CardList = value.Split(",").ToList(); }
}
[XmlIgnore]
公共列表卡片列表{get;private set;}
[XmlAttribute(“卡片”)]
公共字符串卡{
获取{return String.Join(“,”,CardList);}
设置{CardList=value.Split(“,”).ToList();}
}
您不能

您可以做的是创建一个属性,该属性将在其getter/setter中执行此转换

[XmlIgnore]
public List<string> CardList { get; private set; }

[XmlAttribute("cards")]
public string Cards {
   get { return String.Join(",", CardList); }
   set { CardList = value.Split(",").ToList(); }
}
[XmlIgnore]
公共列表卡片列表{get;private set;}
[XmlAttribute(“卡片”)]
公共字符串卡{
获取{return String.Join(“,”,CardList);}
设置{CardList=value.Split(“,”).ToList();}
}

您可以在
IXmlSerializable
的帮助下执行此操作。在网上阅读更多关于它的信息

这边

[Serializable()]
[XmlRoot("Hand")]
public class Hand : IXmlSerializable {
    [XmlAttribute("cards")]
    public List<string> Cards { get; set; }

    public System.Xml.Schema.XmlSchema GetSchema() { return null; }

    public void ReadXml(XmlReader reader) {
        this.Cards = new List<string>(reader.GetAttribute("cards").Split(','));
    }

    public void WriteXml(XmlWriter writer) {
        writer.WriteAttributeString("cards", 
             string.Join(",", 
                this.Cards != null ? this.Cards : new List<string>()));
    }
}
[Serializable()]
[XmlRoot(“手”)]
公共类指针:IXmlSerializable{
[XmlAttribute(“卡片”)]
公共列表卡{get;set;}
public System.Xml.Schema.XmlSchema GetSchema(){return null;}
公共void ReadXml(XmlReader){
this.Cards=新列表(reader.GetAttribute(“Cards”).Split(',');
}
public void WriteXml(XmlWriter){
WriteAttributeString(“卡片”,
string.Join(“,”,
this.Cards!=null?this.Cards:new List());
}
}

希望这对您有所帮助。

您可以在
IXmlSerializable
的帮助下完成这项工作。在网上阅读更多关于它的信息

这边

[Serializable()]
[XmlRoot("Hand")]
public class Hand : IXmlSerializable {
    [XmlAttribute("cards")]
    public List<string> Cards { get; set; }

    public System.Xml.Schema.XmlSchema GetSchema() { return null; }

    public void ReadXml(XmlReader reader) {
        this.Cards = new List<string>(reader.GetAttribute("cards").Split(','));
    }

    public void WriteXml(XmlWriter writer) {
        writer.WriteAttributeString("cards", 
             string.Join(",", 
                this.Cards != null ? this.Cards : new List<string>()));
    }
}
[Serializable()]
[XmlRoot(“手”)]
公共类指针:IXmlSerializable{
[XmlAttribute(“卡片”)]
公共列表卡{get;set;}
public System.Xml.Schema.XmlSchema GetSchema(){return null;}
公共void ReadXml(XmlReader){
this.Cards=新列表(reader.GetAttribute(“Cards”).Split(',');
}
public void WriteXml(XmlWriter){
WriteAttributeString(“卡片”,
string.Join(“,”,
this.Cards!=null?this.Cards:new List());
}
}

希望这对您有所帮助。

这里是MSDN基础知识的一个简单起点,您必须进行自定义序列化。请参阅。这是MSDN基础知识的一个简单起点,您必须进行自定义序列化。请参见.FYI
[Serializable]
未被XML序列化程序使用。@JohnSaunders,右,用于二进制序列化。它在OP的代码中。仅供参考,
[Serializable]
不是由XML序列化程序使用的。@JohnSaunders,右,用于二进制序列化。它在OP的密码里。