J'ai un DataContract qui ressemble à:sérialisation partielle DataContract
[DataContract(Name = User.Root, Namespace = "")]
public class RegisterUser
{
[DataMember(Name = User.EmailAddress)]
public string EmailAddress { get; set; }
[DataMember(Name = User.UserName)]
public string UserName { get; set; }
[DataMember(Name = User.Password)]
public string Password { get; set; }
[DataMember(Name = User.FirstName)]
public string FirstName { get; set; }
[DataMember(Name = User.LastName)]
public string LastName { get; set; }
[DataMember(Name = User.PhoneNumber)]
public string PhoneNumber { get; set; }
[DataMember(Name = "RequestMessage")]
public string RequestMsg { get; set; }
}
Et je voudrais obtenir les éléments hors de lui. Donc, au lieu de <ROOT> <Element1/>...</ROOT>
. Je voudrais juste obtenir <Element1/>
(pour la validation partielle xsd).
Je pensais que je pouvais utiliser cette fonction:
public static string Serialize<T>(T obj)
{
DataContractSerializer ser = new DataContractSerializer(obj.GetType());
String text;
using (MemoryStream memoryStream = new MemoryStream())
{
ser.WriteObject(memoryStream, obj);
byte[] data = new byte[memoryStream.Length];
Array.Copy(memoryStream.GetBuffer(), data, data.Length);
text = Encoding.UTF8.GetString(data);
}
return text;
}
et passer juste
string str = Serialize(test.EmailAddress);
Cela fonctionne très bien, mais le xml ressemble:
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">myemail.com</string>"
J'ai perdu le DataMember Info. Comment puis-je conserver cela aussi?