C# 如何反序列化具有不同标记名但子标记相同的XML?
我有以下XML:C# 如何反序列化具有不同标记名但子标记相同的XML?,c#,xml,deserialization,xmlserializer,C#,Xml,Deserialization,Xmlserializer,我有以下XML: <Vehicle> <Type> <ISN>213123214</ISN> <Name>ddsd</Name> </Type> <RegNo>1234</RegNo> <Mark> <ISN>423434234</ISN> &
<Vehicle>
<Type>
<ISN>213123214</ISN>
<Name>ddsd</Name>
</Type>
<RegNo>1234</RegNo>
<Mark>
<ISN>423434234</ISN>
<Name>asdasd</Name>
</Mark>
<Model>
<ISN>434234324324</ISN>
<Name>asddsa</Name>
</Model>
<EstimatedPrice>
<Amount>15000</Amount>
<AmountPrev />
<Currency>
<Code>R</Code>
<Name>RU</Name>
</Currency>
</EstimatedPrice>
</Vehicle>
嗯,我正在尝试使用C#将其反序列化到目标对象。
这是我的目标课程:
public class Vehicle {
[XmlElement("Type")]
public Type Type { get; set; }
[XmlElement("Mark")]
public Mark Brand { get; set; }
[XmlElement("Model")]
public Mark Brand { get; set; }
[XmlElement("EstimatedPrice")]
public Estimation Estimation { get; set; }
}
问题是如何正确地反序列化属性标记、模型和类型?现在我得到的是空对象而不是数据。我试图将“Vehicle”指定为这个子类的根标记,但没有效果
p.S.类别标记、型号和类型源自基本类别:
[Serializable]
public class ResponseItem
{
[XmlElement("ISN")]
string ISN { get; set; }
[XmlElement("Name")]
string Name { get; set; }
}
对于反序列化,我使用XmlSerializer类。所有具有to子标记或具有唯一名称的子标记的标记都已正确反序列化
我错过了什么
谢谢。您的
响应项中的属性必须是公共的
public class ResponseItem
{
[XmlElement("ISN")]
public string ISN { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
}
XML序列化程序不会处理非公共属性我喜欢反向工作并创建测试序列化程序来验证我的结构
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
Vehicle vehicle = new Vehicle() {
Type = new Type()
{
ISN = "213123214",
Name = "ddsd"
},
RegNo = 1234,
Brand = new Mark()
{
ISN = "423434234",
Name = "asdasd"
},
Model = new Mark()
{
ISN = "434234324324",
Name = "asddsa"
},
estimation = new Estimation()
{
Amount = 15000,
AmountPrev = null,
currency = new Currency()
{
Code = "R",
Name = "RU"
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Vehicle));
StreamWriter writer = new StreamWriter(FILENAME);
serializer.Serialize(writer, vehicle);
writer.Flush();
writer.Close();
writer.Dispose();
}
}
[XmlRoot("Vehicle")]
public class Vehicle
{
[XmlElement("Type")]
public Type Type { get; set; }
[XmlElement("Mark")]
public Mark Brand { get; set; }
[XmlElement("Model")]
public Mark Model { get; set; }
[XmlElement("RegNo")]
public int RegNo { get; set; }
[XmlElement("EstimatedPrice")]
public Estimation estimation { get; set; }
}
[XmlRoot("EstimationPrice")]
public class Estimation
{
[XmlElement("Amount")]
public int Amount { get; set; }
[XmlElement("AmountPrev")]
public int? AmountPrev { get; set; }
[XmlElement("Currency")]
public Currency currency { get; set; }
}
[XmlRoot("Currency")]
public class Currency
{
[XmlElement("Code")]
public string Code { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
}
[XmlRoot("Type")]
public class Type : ResponseItem
{
}
[XmlRoot("Mark")]
public class Mark : ResponseItem
{
}
[XmlRoot("ResponseItem")]
public class ResponseItem
{
[XmlElement("ISN")]
public string ISN { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
}
}
显示完整有效的代码。这甚至不会编译-请参阅复制的公共标记品牌{get;set;}
属性。请注意,[Serializable]
与XML序列化无关。好吧,是我的错。您只需删除重复的标记即可。若你们给我一个只有一个字段的例子,对我来说就足够了class Mark:ResponseItem{}class Type:ResponseItem{}
EstimatedPrice标记也是不必要的,所以标记'EstimatedPrice'我总是先为XML定义XSD,然后使用它使用Microsoft的XSD.exe工具生成要序列化和反序列化的类。@user3548735请编辑您的问题并提供一个最小值,完整的、可验证的示例:这对于XML序列化尤其重要——魔鬼在细节中,而且通常被深深地埋藏着。这就是问题所在!