C#-XmlSerializer只获取第一个XML元素
我需要序列化XML字符串并将其绑定到类。问题是,C#-XmlSerializer只获取第一个XML元素,c#,xml,serialization,C#,Xml,Serialization,我需要序列化XML字符串并将其绑定到类。问题是,XmlSerializer只获取XML字符串的第一个元素 var xml = @"<cars> <car> <make>Ford</make> <model>Mustang</model> <color>R
XmlSerializer
只获取XML字符串的第一个元素
var xml = @"<cars>
<car>
<make>Ford</make>
<model>Mustang</model>
<color>Red</color>
</car>
<car>
<make>Infiniti</make>
<model>G35</model>
<color>Red</color>
</car>
</cars>";
using (var reader = new StringReader(xml))
{
var deserializer = new XmlSerializer(typeof(cars), new XmlRootAttribute("cars"));
var r = (cars)deserializer.Deserialize(reader);
}
我试着改变这个
public carsCar car { get; set; }
为此:
public List<carsCar> car { get; set; }
public List car{get;set;}
但是我的
列表是空的。如果您将xml复制到剪贴板,然后在Visual Studio中,转到“编辑”、“粘贴特殊”、“将xml粘贴为类”,它将生成正确的类结构。您可以调整这些类以获得所需的名称和数据类型。然后,您的代码将工作,r.car
将是一个汽车数组
//注意:生成的代码可能至少需要.NET Framework 4.5或.NET Core/Standard 2.0。
///
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分车厢
{
私人卡菲尔德;
///
[System.Xml.Serialization.XmlElementAttribute(“car”)]
公共汽车
{
得到
{
把这个还给卡菲尔德;
}
设置
{
this.carField=值;
}
}
}
///
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类carsCar
{
私有字符串makeField;
私有字符串模型字段;
私有字符串颜色域;
///
公共字符串生成
{
得到
{
返回这个.makeField;
}
设置
{
this.makeField=值;
}
}
///
公共字符串模型
{
得到
{
返回此.modelField;
}
设置
{
this.modelField=值;
}
}
///
公共字符串颜色
{
得到
{
返回这个.colorField;
}
设置
{
this.colorField=值;
}
}
}
如果将xml复制到剪贴板,然后在Visual Studio中转到“编辑”、“粘贴特殊”和“将xml粘贴为类”,它将生成正确的类结构。您可以调整这些类以获得所需的名称和数据类型。然后,您的代码将工作,r.car
将是一个汽车数组
//注意:生成的代码可能至少需要.NET Framework 4.5或.NET Core/Standard 2.0。
///
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分车厢
{
私人卡菲尔德;
///
[System.Xml.Serialization.XmlElementAttribute(“car”)]
公共汽车
{
得到
{
把这个还给卡菲尔德;
}
设置
{
this.carField=值;
}
}
}
///
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类carsCar
{
私有字符串makeField;
私有字符串模型字段;
私有字符串颜色域;
///
公共字符串生成
{
得到
{
返回这个.makeField;
}
设置
{
this.makeField=值;
}
}
///
公共字符串模型
{
得到
{
返回此.modelField;
}
设置
{
this.modelField=值;
}
}
///
公共字符串颜色
{
得到
{
返回这个.colorField;
}
设置
{
this.colorField=值;
}
}
}
对你有用吗?这正是我所做的,但正如我在帖子中提到的。r、 由于某种原因,这辆车是空的。如果我制作一个列表或数组,它们是空的。我用c#Paste生成了这个类Special@KidBilly是的,请参见使用屏幕快照编辑OK谢谢。我重新生成了这个类,它成功了。当我改变名字并删除私有字段时,我一定把课堂上的事情搞砸了。我现在得到的结果和你一样。谢谢那对你有用吗?这正是我所做的,但正如我在帖子中提到的。r、 由于某种原因,这辆车是空的。如果我制作一个列表或数组,它们是空的。我用c#Paste生成了这个类Special@KidBilly是的,请参见使用屏幕快照编辑OK谢谢。我重新生成了这个类,它成功了。当我改变名字并删除私有字段时,我一定把课堂上的事情搞砸了。我现在得到的结果和你一样。谢谢
public List<carsCar> car { get; set; }
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class cars
{
private carsCar[] carField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("car")]
public carsCar[] car
{
get
{
return this.carField;
}
set
{
this.carField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class carsCar
{
private string makeField;
private string modelField;
private string colorField;
/// <remarks/>
public string make
{
get
{
return this.makeField;
}
set
{
this.makeField = value;
}
}
/// <remarks/>
public string model
{
get
{
return this.modelField;
}
set
{
this.modelField = value;
}
}
/// <remarks/>
public string color
{
get
{
return this.colorField;
}
set
{
this.colorField = value;
}
}
}