C# serializing Class to XML where one of class properties is DateTime. How to make this property in ISO format? - datetime

I'm serializing class which contains DateTime property.
public DateTime? Delivered { get; set; }
After serializing Delivered node contains DateTime formatted like this:
2008-11-20T00:00:00
How can I change this property to make it look like this:
2008-11-20 00:00:00
Thanks in advance

The hack I use for odd formatting during XmlSerialization is to have a special property that is only used during XmlSerialization
//normal DateTime accessor
[XmlIgnore]
public DateTime Delivered { get; set; }
//special XmlSerialization accessor
[XmlAttribute("DateTime")]
public string XmlDateTime
{
get { return this.Delivered.ToString("o"); }
set { this.Delivered = new DateTime.Parse(value); }
}

Take a look at XmlAttributeOverrides class.

Related

Is there a way to change Property Name in JSON data

Is there a way I can change the column/property name I'm returning from a JSON response through a Web API ?
Sample JSON response:
[
{
"ActualPropName1": "Value1", //change ActualPropName1 to something else
"ActualPropName2": "Value2"
}
]
I tried using Data Annotation on the Model, but, it doesn't seem to achieve what I want
[DisplayName("Generic Name")]
public string ActualPropName1{ get; set; }
You could simply have a new property with the desired name that simply returns data from the other property. Then hide/ignore the other:
[JsonIgnore]
public string ActualPropName1 { get; set; }
public string GenericName
{
get
{
return ActualPropName1;
}
}
Although from your example this won't work for property names like 'Generic Name'.
You can use JSON.NET's JsonProperty
[JsonProperty(PropertyName="Your_Name")]
public string ActualPropName1{ get; set; }

Validating that a Reason field is filled if Date field is today

I use ASP.NET MVC4 in my solution. I have the ViewModel below where I would like to validate that the field EmergencyReason is filled only if the field Date is today. I try this:
public class LoadingViewModel
{
public DateTime Date { get; set; }
[RequiredIf("Date", Comparison.IsEqualTo, DateTime.Today)]
public string EmergencyReason { get; set; }
...
}
It doesn't work. The third argument of RequiredIf must be a constant expression, ...
Any idea how can I force the user to enter an EmergencyReason only if Date field is today?
Thanks.
You seem to be using some non-standard RequiredIf attribute which is not part of the standard ASP.NET MVC 4 package.
As you know C# allows you to only pass constant values to attributes. So one possibility is to write a custom attribute:
public class RequiredIfEqualToTodayAttribute: RequiredIfAttribute
{
public RequiredIfEqualToTodayAttribute(string field)
: base(field, Comparison.IsEqualTo, DateTime.Today)
{
}
}
and then:
public class LoadingViewModel
{
public DateTime Date { get; set; }
[RequiredIfEqualToToday("Date")]
public string EmergencyReason { get; set; }
...
}
C# doesn't support DateTime literals, a workaround for this is to use a String like this, but it won't resolve your problem. I suggest you move the validation code inside the Controller and return a ModelState.AddModelError("EmergencyReason", "Emergency Reason is required")

Specify JsonConverters with attributes in Json.net?

I am receiving JSON data to my server with dates represented like this:
{
"startDate": {
"d": "/Date(1346454000000)/"
},
"slots": [
{
"d": "/Date(1347058800000)/"
},
{
"d": "/Date(1347145200000)/"
}
]
}
Its serialized to a simple object:
public class SlotsVm
{
public DateTime StartDate { get; set; }
public DateTime[] Slots { get; set; }
}
Because the date format is strange, I had to write a custom JsonConverter to process it. To deserialize, I use code like this:
var slotsVm = JsonConvert.DeserializeObject<SlotsVm>(body,
new CustomDateTimeConverter());
If possible, I would like the need for the converter to be defined on the SlotsVm class, rather than in the code that actually makes the conversion. This is possible for the startDate property using attributes:
[JsonConverter(typeof(CustomDateTimeConverter))]
public DateTime StartDate { get; set; }
but it is not possible for Slots, which is an array instead of a simple DateTime.
It would be best for me to be able to define the converters that the class needs on the class itself:
[JsonConverters(typeof(CustomDateTimeConverter), ...]
public class PutDealVm
{
}
but there doesn't seem to be a way to do this.
Can you think of a solution? Is there some way to define converters for a class that I have missed? Alternatively, is it possible to define the converter that an array should user for each of its elements?
What about something like this? I appreciate that this may not directly answer your question, but I was too lazy to recreate the CustomDateTimeConverter class, so came up with this instead, which is arguably simpler, as there is no need for a custom converter.
string json = "{\"startDate\": {\"d\": \"/Date(1346454000000)/\"},\"slots\": [{\"d\": \"/Date(1347058800000)/\"},{\"d\": \"/Date(1347145200000)/\"}]}";
SlotsVmDeserialized slotsVmDeserialized =
JsonConvert.DeserializeObject<SlotsVmDeserialized>(json);
SlotsVm slotsVm = new SlotsVm()
{
Slots = slotsVmDeserialized.Slots.Select(d => d.Date),
StartDate = slotsVmDeserialized.StartDate.Date
};
....
public class SlotsVm
{
public DateTime StartDate { get; set; }
public IEnumerable<DateTime> Slots { get; set; }
}
public class SlotsVmDeserialized
{
public DateObject StartDate { get; set; }
public IEnumerable<DateObject> Slots { get; set; }
}
public class DateObject
{
[JsonProperty("d")]
public DateTime Date { get; set; }
}
What I've done here is firstly deserialized to the SlotsVmDeserialized class, which is used to hold a single DataObject and an IEnumerable of them. The DateObject class is essentially my alternative/workaround to the custom converter. I could have done all this without any attributes by removing [JsonProperty("d")] and renaming Data to D.
Once deserialized, I then create a new SlotsVm object based on the properties of the SlotsVmDeserialized object.

Map all properties of a class using reflection

I have two domain classes
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string HouseName { get; set; }
public string StreetName { get; set; }
public string PinCode { get; set; }
}
I want to map object of Employee class to another class.
I am using reflection to map empData object to another object. The code i used is
private void GetValues(object empData)
{
System.Type type = empData.GetType();
foreach (PropertyInfo pInfo in type.GetProperties())
{
//do some stuff using this pInfo.
}
}
I could easily map all the properties except the Address property in the emp object which is an object of another class.
So how can i map all the properties irrespective of its type ? i.e, if address contains object of another class it should also get mapped.
Can't you use AutoMapper for mapping classes?
You can know the type of property you are mapping by
if (propertyInfo.PropertyType == typeof(Address))
{ // do now get all properties of this object and map them}
Assuming that you want to be able to do this on any type of object and not just this specific one, you should use some sort of recursive solution. However if it's just for this object - why are you even using reflection? To me it just adds unnecessary complexity to something as simple as mapping six properties to another set of objects.
If you want to get more concrete help with code examples, you'll have to give us some more context. Why does a method named "GetValues" has a return type of void? I have a hard time coding up an example with that in mind. :)

Databinding to the DataGridView (Enums + Collections)

I'm after a little help with the techniques to use for Databinding. It's been quite a while since I used any proper data binding and want to try and do something with the DataGridView. I'm trying to configure as much as possible so that I can simply designed the DatagridView through the form editor, and then use a custom class that exposes all my information.
The sort of information I've got is as follows:
public class Result
{
public String Name { get; set; }
public Boolean PK { get; set; }
public MyEnum EnumValue { get; set; }
public IList<ResultInfos> { get; set; }
}
public class ResultInfos { get; set; }
{
public class Name { get; set; }
public Int Value { get; set; }
public override String ToString() { return Name + " : " Value.ToString(); }
}
I can bind to the simple information without any problem. I want to bind to the EnumValue with a DataGridViewComboBoxColumn, but when I set the DataPropertyName I get exceptions saying the enum values aren't valid.
Then comes the ResultInfo collection. Currently I can't figure out how to bind to this and display my items, again really I want this to be a combobox, where the 1st Item is selected. Anyone any suggestions on what I'm doing wrong?
Thanks
Before you bind your data to the grid, first set the DataGridViewComboBoxColumn.DataSource like this...
combo.DataSource = Enum.GetValues(typeof(YourEnum));
I generally do this in the constructor after InitializeComponent(). Once this is set up you will not get an exception from the combo column when you bind your data. You can set DataGridViewComboBoxColumn.DataPropertyName at design time as normal.
The reason you get an exception when binding without this step is that the cell tries to select the value from the list that matches the value on the item. Since there are no values in the list... it throws an exception.

Resources