I am trying to change Xml element name based on a rule during serialization.
Of course,I can manually add ElementName attribute manually but I have more than 1000 property in my class to be serialized.
In addition to that I don't want to change all property names only some of them which don't have Xml ElementName attribute.
For Instance ;
public class ToBeSerialized()
{
public string FirstName {get;set;}
public string LastName {get;set;}
[XmlElement(ElementName = "MyHome")]
public string Home {get;set;}
}
My Naming Convention for the ones don't have ElementName attribute.
public string ChangeName(string item)
{
if (!propName.Any(p => char.IsLower(p)))
{
return propName;
}
var builder = new StringBuilder();
for (int i = 0; i < propName.Length; i++)
{
if (i != 0 && char.IsUpper(propName[i]))
{
builder.Append("_");
}
builder.Append(propName[i].ToString().ToUpper(new CultureInfo("en-US", false)));
}
return builder.ToString();
}
Desired Xml
<ToBeSerialized First_Name="Name" Last_Name="Surname" MyHome="Mine" />
Aucun commentaire:
Enregistrer un commentaire