我正在使用asp.net(C#)网站,我希望使用Deserialize
类XML
XmlSerializer
字符串。
我的模型(.cs文件)
[XmlRoot("MedicalClearanceFormRoot")]
public class MedicalClearanceViewModel
{
[XmlAttribute("PassengerName")]
public string PassengerName { get; set; }
[XmlAttribute("Gender")]
public string Gender { get; set; }
[XmlAttribute("Age")]
public string Age { get; set; }
[XmlAttribute("PhoneNo")]
public string PhoneNo { get; set; }
[XmlAttribute("Email")]
public string Email { get; set; }
[XmlAttribute("BookingRefNo")]
public string BookingRefNo { get; set; }
}
XML字符串
<MedicalClearanceFormRoot>
<MedicalClearanceForm PassengerName="AAAAAAAAAAAAA" Age="11" PhoneNo="TTTTTTTTTTT" Email="ZZZZZZZZZZZZZZZZZZZ" BookingRefNo="11111111111111111111" />
</MedicalClearanceFormRoot>
将XML反序列化为对象的代码
string myXMLStringFromDB = GetXMLStringFromDb(); // this method will return XML from db.
XmlSerializer serializer = new XmlSerializer(typeof(MedicalClearanceViewModel));
using (TextReader reader = new StringReader(myXMLStringFromDB))
{
MedicalClearanceViewModel objModel = (MedicalClearanceViewModel)serializer.Deserialize(reader);
}
但问题是,当我使用上面的代码将XML反序列化为对象时... PassengerName
,Age
,PhoneNo
等属性在{ {1}}
有人可以帮助我在我的班级上设置正确的XML符号,有人可以帮我解决这个问题。
任何帮助将受到高度赞赏! 谢谢
答案 0 :(得分:1)
我创建了一个示例,代码将完全如下所示。你的模型不正确。
public class MedicalClearanceForm
{
[XmlAttribute("PassengerName")]
public string PassengerName { get; set; }
[XmlAttribute("Gender")]
public string Gender { get; set; }
[XmlAttribute("Age")]
public string Age { get; set; }
[XmlAttribute("PhoneNo")]
public string PhoneNo { get; set; }
[XmlAttribute("Email")]
public string Email { get; set; }
[XmlAttribute("BookingRefNo")]
public string BookingRefNo { get; set; }
}
[XmlRoot("MedicalClearanceFormRoot")]
public class MedicalClearanceFormRoot
{
[XmlElement("MedicalClearanceForm")]
public MedicalClearanceForm MedicalClearanceForm { get; set; }
}
class Program
{
static void Main(string[] args)
{
string myXMLStringFromDB = @"<MedicalClearanceFormRoot><MedicalClearanceForm PassengerName = 'AAAAAAAAAAAAA' Age = '11' PhoneNo = 'TTTTTTTTTTT' Email = 'ZZZZZZZZZZZZZZZZZZZ' BookingRefNo = '11111111111111111111' /></MedicalClearanceFormRoot >";
XmlSerializer serializer = new XmlSerializer(typeof(MedicalClearanceFormRoot));
using (TextReader reader = new StringReader(myXMLStringFromDB))
{
MedicalClearanceFormRoot objModel = (MedicalClearanceFormRoot)serializer.Deserialize(reader);
}
}
}
答案 1 :(得分:0)
为了满足指定的XML,我相信你需要这个类结构。
[XmlRoot("MedicalClearanceFormRoot")]
public class MedicalClearanceViewModel
{
public MedicalClearanceFormElement MedicalClearanceForm { get; set; }
}
[XmlElement]
public class MedicalClearanceFormElement
{
[XmlAttribute("PassengerName")]
public string PassengerName { get; set; }
[XmlAttribute("Gender")]
public string Gender { get; set; }
[XmlAttribute("Age")]
public string Age { get; set; }
[XmlAttribute("PhoneNo")]
public string PhoneNo { get; set; }
[XmlAttribute("Email")]
public string Email { get; set; }
[XmlAttribute("BookingRefNo")]
public string BookingRefNo { get; set; }
}