无法反序列化多层 XML
Unable to deserialize multilayered XML
当我从下面删除响应数据标签时 XML 我得到了正确的结果。
string xmlString = "<BIOMETRIC_VERIFICATION><RESPONSE_DATA><RESPONSE_STATUS><CODE>103</CODE>
<MESSAGE>invalid user name/password</MESSAGE></RESPONSE_STATUS></RESPONSE_DATA></BIOMETRIC_VERIFICATION>";
XmlSerializer serializer = new XmlSerializer(typeof(List<Product>), new XmlRootAttribute("Products"));
StringReader stringReader = new StringReader(xmlString);
List<Product> productList = (List<Product>)serializer.Deserialize(stringReader);
Console.WriteLine("Records in list: " + productList.Count);
productList.ForEach(item => Console.WriteLine(item.CODE + " - " + item.MESSAGE));
}
public class Product
{
public int CODE{ get; set; }
public string MESSAGE{ get; set; }
}
}
根据您的 xml 模型将如下所示。我已经使用 https://xmltocsharp.azurewebsites.net/ 生成基于 xml
的模型
[XmlRoot(ElementName = "RESPONSE_STATUS")]
public class RESPONSE_STATUS
{
[XmlElement(ElementName = "CODE")]
public string CODE { get; set; }
[XmlElement(ElementName = "MESSAGE")]
public string MESSAGE { get; set; }
}
[XmlRoot(ElementName = "RESPONSE_DATA")]
public class RESPONSE_DATA
{
[XmlElement(ElementName = "RESPONSE_STATUS")]
public RESPONSE_STATUS RESPONSE_STATUS { get; set; }
}
[XmlRoot(ElementName = "BIOMETRIC_VERIFICATION")]
public class BIOMETRIC_VERIFICATION
{
[XmlElement(ElementName = "RESPONSE_DATA")]
public RESPONSE_DATA RESPONSE_DATA { get; set; }
}
序列化程序应该是
var xmlString = File.ReadAllText("XMLFile1.xml");
XmlSerializer serializer = new XmlSerializer(typeof(BIOMETRIC_VERIFICATION), new XmlRootAttribute("BIOMETRIC_VERIFICATION"));
StringReader stringReader = new StringReader(xmlString);
var bv = (BIOMETRIC_VERIFICATION)serializer.Deserialize(stringReader);
Console.WriteLine(bv.RESPONSE_DATA.RESPONSE_STATUS.CODE + " - " + bv.RESPONSE_DATA.RESPONSE_STATUS.MESSAGE);
当我从下面删除响应数据标签时 XML 我得到了正确的结果。
string xmlString = "<BIOMETRIC_VERIFICATION><RESPONSE_DATA><RESPONSE_STATUS><CODE>103</CODE>
<MESSAGE>invalid user name/password</MESSAGE></RESPONSE_STATUS></RESPONSE_DATA></BIOMETRIC_VERIFICATION>";
XmlSerializer serializer = new XmlSerializer(typeof(List<Product>), new XmlRootAttribute("Products"));
StringReader stringReader = new StringReader(xmlString);
List<Product> productList = (List<Product>)serializer.Deserialize(stringReader);
Console.WriteLine("Records in list: " + productList.Count);
productList.ForEach(item => Console.WriteLine(item.CODE + " - " + item.MESSAGE));
}
public class Product
{
public int CODE{ get; set; }
public string MESSAGE{ get; set; }
}
}
根据您的 xml 模型将如下所示。我已经使用 https://xmltocsharp.azurewebsites.net/ 生成基于 xml
的模型[XmlRoot(ElementName = "RESPONSE_STATUS")]
public class RESPONSE_STATUS
{
[XmlElement(ElementName = "CODE")]
public string CODE { get; set; }
[XmlElement(ElementName = "MESSAGE")]
public string MESSAGE { get; set; }
}
[XmlRoot(ElementName = "RESPONSE_DATA")]
public class RESPONSE_DATA
{
[XmlElement(ElementName = "RESPONSE_STATUS")]
public RESPONSE_STATUS RESPONSE_STATUS { get; set; }
}
[XmlRoot(ElementName = "BIOMETRIC_VERIFICATION")]
public class BIOMETRIC_VERIFICATION
{
[XmlElement(ElementName = "RESPONSE_DATA")]
public RESPONSE_DATA RESPONSE_DATA { get; set; }
}
序列化程序应该是
var xmlString = File.ReadAllText("XMLFile1.xml");
XmlSerializer serializer = new XmlSerializer(typeof(BIOMETRIC_VERIFICATION), new XmlRootAttribute("BIOMETRIC_VERIFICATION"));
StringReader stringReader = new StringReader(xmlString);
var bv = (BIOMETRIC_VERIFICATION)serializer.Deserialize(stringReader);
Console.WriteLine(bv.RESPONSE_DATA.RESPONSE_STATUS.CODE + " - " + bv.RESPONSE_DATA.RESPONSE_STATUS.MESSAGE);