Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, December 17, 2014 12:45 PM
Hi All,
Below i am giving you one code snippet.
Input Parameters
inputData : input xml .
wcfProxyAsm : Dll Name.
inputTypeName : Namespace to find exact TYPE in dll.
1) object objInputClass = DeserializeObject(inputData, wcfProxyAsm.GetType(inputTypeName, true, true));
2) private static Object DeserializeObject(string pXmlizedString, Type T)
{
using (StringReader strReader = new StringReader(pXmlizedString))
{
System.Runtime.Serialization.DataContractSerializer deserializer = new
System.Runtime.Serialization.DataContractSerializer(T);
XmlReader xmlreader = XmlReader.Create(strReader);
return deserializer.ReadObject(xmlreader);
}
}
By using code snippet no. 1, I get the object with null properties and without null properties.
My aim is to ignore all null properties from object.
How can i achieve this?
Thank you,
Amol Shinde
All replies (11)
Thursday, December 25, 2014 1:00 PM âś…Answered
Comon man, if the data is null value, then you do nothing to update the value of the data you are trying to replace you can't remove the property. You can check the property for null value and do nothing to update the data.
Wednesday, December 17, 2014 1:34 PM
When you say ignore null properties, what do you mean?
If the idea is to get something which is a specific type then it has to have the properties it's defined as having.
It won't be that type if you make something which doesn't have some properties.
If you are instead deserialising as a var then when you deserialize it won't have properties that aren't in the xml.
Please don't forget to upvote posts which you like and mark those which answer your question.
My latest Technet article - Dynamic XAML
Wednesday, December 17, 2014 2:13 PM
Hi Andy,
Thanks for reply.
Suppose i have following object.
objCustomer-->
Name : Michel
City : null
Then i want to ignore/remove the 'City' property/variable from object.
How can i achieve this?
Thank you,
Amol Shinde
Wednesday, December 17, 2014 3:08 PM
Then i want to ignore/remove the 'City' property/variable from object.
How can i achieve this?
You can't do it. You can intercept the serialized object that is now XML and remove the City <tag>. But who would even what to do such a thing? You try to deserialize the XML back into an object, and it might blow up. You can try it. The concrete class/object itself will still have the property in it no matter what you do to the XML tags that represents the concrete class/object properties when the XML is cast back to the object.
Wednesday, December 17, 2014 3:24 PM
You could try to set the EmitDefaultValue property of the DataMember attribure for the City property of the Customer class to false, e.g:
[DataMember(EmitDefaultValue=false)]
public string City {get; set;}
Please remember to mark helpful posts as answer and/or helpful.
Wednesday, December 17, 2014 4:02 PM
You could try to set the EmitDefaultValue property of the DataMember attribure for the City property of the Customer class to false, e.g:
[DataMember(EmitDefaultValue=false)] public string City {get; set;}
Please remember to mark helpful posts as answer and/or helpful.
That won't stop a null property being produced.
All it does is say whether the default value is used or not.
Please don't forget to upvote posts which you like and mark those which answer your question.
My latest Technet article - Dynamic XAML
Wednesday, December 17, 2014 4:17 PM
Hi Andy,
Thanks for reply.
Suppose i have following object.
objCustomer-->
Name : Michel
City : nullThen i want to ignore/remove the 'City' property/variable from object.
How can i achieve this?Thank you,
Amol Shinde
You would have to build a custom type or a dynamic type rather than that fixed type.
Asuming you are going to deserialize.
The properties are a fundamental part of a type.
If it doesn't have a City property and your type says it has a City property then it isn't that type and when you deserialize it will just get a null value in City.
.
So how about if you don't deserialize?
If you don't end up with an object there then working with your data in code is probably going to be a nuisance.
.
You can write xml which has only properties which aren't null, that's possible.
What you do when you deserialize them is your problem.
If you can use the data just as a set of Xelements with values in them then that could work.
Depends on your purpose.
But say you build some xml had a bunch of nodes called Address
Very roughly.
<Addresses>
<Address>
...
</Address>
<Address>
Then you can build the contents of each Address node ( Xelement) that by iterating the properties of some Address object and only adding an XElement to that address XElement for a property which isn't null.
XML won't care about that.
You can then get the data back out using linq to xml.
Like I said though.
Once you have that data back out processing will be a nuisance.
Unless working with XElements suits your purpose.
If it doesn't then you're best just living with nulls.
Please don't forget to upvote posts which you like and mark those which answer your question.
My latest Technet article - Dynamic XAML
Wednesday, December 17, 2014 4:19 PM
>>That won't stop a null property being produced.
The property will of course not disappear from the type specified by the inputTypeName parameter, i.e if you have a type like this in the wcfProxyAsm assembly there is no way to remove the City property from the type itself if that's what you are asking:
[DataContract(Name="User", Namespace="")]
public class User
{
[DataMember]
public string City {
get;
set;
}
[DataMember(EmitDefaultValue = false)]
public string Name {
get;
set;
}
}
You cannot change the definition of a compile-time type at runtime.
Please remember to mark helpful posts as answer to close the thread.
Thursday, December 18, 2014 7:50 AM
Hi,
I am not quite sure why you want to remove that property in that object, but in the type system you really cannot do that. Hope you can share the reason for the removal.
The only think i could think of is to make use of Reflection together with Dynamic object and create a new object from there.
http://msdn.microsoft.com/en-us/library/z919e8tw.aspx
http://msdn.microsoft.com/en-us/library/dd264736.aspx
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Thursday, December 25, 2014 12:37 PM
Hi Giovhan,
I am updating that object on server, but due to null values associated with properties, original/old data updates with null data.
Thats why i want to remove null property from object.
Saturday, December 27, 2014 3:41 AM
I agree with darnold924, you just have to check for null value for each property. theres nothing wrong with that.
As to your problem to remove object I think the answer is pretty clear on above answer but not worth it when you are just checking for nullity.
Good luck.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.