I have a xml string in a json data field. I want to extract that value and compare that to the database's value for that field.
I used xsd.exe to generate the class for that xml (saw from here ). I am using that class to deserialize the xml response. Then i used the method from here
to deserialize.
I used
`XmlSerializer serializer1 = new XmlSerializer(typeof(class_gen_from_xml))
In the below code, I extracted the xml source from the json response and then did as below:
string xmlSource = "<ResultSet><Result precision=\"address\"> <Latitude>47.643727</Latitude></Result></ResultSet>";
XmlSerializer serializer = new XmlSerializer(typeof(ResultSet));
ResultSet output;
using (StringReader reader = new StringReader(xmlSource))
{
output = (ResultSet)serializer.Deserialize(reader);
}
` And I am getting an Exception and debugging reveals nothing at all. Is there something I am missing in the code ?
Probably something wrong with your ResultSet
class, this works fine for me:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ResultSet
{
private ResultSetResult[] resultField;
[System.Xml.Serialization.XmlElementAttribute("Result")]
public ResultSetResult[] Result
{
get
{
return this.resultField;
}
set
{
this.resultField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResultSetResult
{
private decimal latitudeField;
private string precisionField;
public decimal Latitude
{
get
{
return this.latitudeField;
}
set
{
this.latitudeField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string precision
{
get
{
return this.precisionField;
}
set
{
this.precisionField = value;
}
}
}
With your de-serialization code:
static void Main(string[] args)
{
string xmlSource = "<ResultSet><Result precision=\"address\"> <Latitude>47.643727</Latitude></Result></ResultSet>";
XmlSerializer serializer = new XmlSerializer(typeof(ResultSet));
ResultSet output;
using (StringReader reader = new StringReader(xmlSource))
{
output = (ResultSet)serializer.Deserialize(reader);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.