问题
I have a stored procedure in the database which returns an XML stream, and the application deserializes that stream into its corresponding object. The stored procedure is defined like this (I simplified it to make it more readable):
SELECT
usrs.FirstName AS 'FirstName',
usrs.LastName AS 'LastName',
usrs.Username AS 'Username',
usrs.DateJoined AS 'DateJoined'
FROM USERS AS usrs
WHERE usrs.Username = @username
FOR XML PATH('UserProfile')
Notice that Username
is a primary key, so the stored procedure will return only one result. A sample query result would look like this:
<UserProfile>
<FirstName>Chuck</FirstName>
<LastName>Norris</LastName>
<Username>chuck.awesome</Username>
<DateJoined>2013-07-22T06:58:00</DateJoined>
</UserProfile>
Now in the application, this is how I get and deserialize the data:
internal static T GetData<T>(StoredProcedures storedProcedure, ParameterList parameters)
{
using (var connection = GetSqlConnection())
{
using (var command = new SqlCommand(storedProcedure.ToString(), connection))
{
command.CommandType = System.Data.CommandType.StoredProcedure;
foreach (var parameter in parameters)
{
command.Parameters.Add(new SqlParameter(parameter.Key.ToString(), parameter.Value));
}
connection.Open();
var data = command.ExecuteScalar();
return DeserializeXml<T>(data.ToString());
}
}
}
And the DeserializeXML<T>()
method:
private static T DeserializeXml<T>(string xmlStream, Type[] additionalTypes = null)
{
XmlSerializer serializer;
if (additionalTypes == null)
{
serializer = new XmlSerializer(typeof(T));
}
else
{
serializer = new XmlSerializer(typeof(T), additionalTypes);
}
using (StringReader reader = new StringReader(xmlStream))
{
return (T)serializer.Deserialize(reader);
}
}
And finally the UserProfile
class:
[XmlRoot("UserProfile")]
[Serializable]
public class UserProfile
{
public UserProfile()
{
}
[XmlAttribute("Username")]
public string Username { get; set; }
[XmlAttribute("FirstName")]
public string FirstName { get; set; }
[XmlAttribute("LastName")]
public string LastName { get; set; }
[XmlAttribute("DateJoined")]
public DateTime DateJoined { get; set; }
}
Now when I run the application, I see that the stored procedure returns the expected value, however, the serializer returns a UserProfile
object with all fields set to null
(except for the DateJoined
field, which is set to the default value since it's not nullable). Any idea what could be going wrong? I suspect it might be the XmlRoot()
attribute in the UserProfile
object, but then again the serializer doesn't throw any exception which is why I'm confused. Any idea what might be going wrong? Thanks in advance.
回答1:
You've marked the properties with [XmlAttribute]
but the xml contains those values as elements not attributes.
回答2:
I had the same problem of deserialization being returned as null. I had wrongly typed the Element name as below [XmlElement(ElementName = "fname")]
The correct one was - [XmlElement(ElementName = "firstname")]
Just as FYI if anybody does this mistake.
来源:https://stackoverflow.com/questions/17817414/xml-serializer-returns-null-on-object-deserialization