send null values during serialisation - vb.net

Trying to determine best way to seralize so I can send in a null value for middle name if not entered
My xml:
<Contact>
<ID />
<First_Name />
<Middle_Name xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:nil="true" />
<Last_Name />
<TelephoneNumbers>
<TelephoneNumber>
<Number />
<IsHome />
<IsWork />
<IsCell />
<ReachableAfterHrs />
</TelephoneNumber>
</TelephoneNumbers>
</Contact>
The vb class created for serialization/deserialisation
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class Contact
Private idField As String
Private first_NameField As String
Private middle_NameField As ContactMiddle_Name
Private last_NameField As String
Private telephoneNumbersField As ContactTelephoneNumbers
'''<remarks/>
Public Property ID() As String
Get
Return Me.idField
End Get
Set(value As String)
Me.idField = Value
End Set
End Property
'''<remarks/>
Public Property First_Name() As String
Get
Return Me.first_NameField
End Get
Set(value As String)
Me.first_NameField = Value
End Set
End Property
'''<remarks/>
Public Property Middle_Name() As ContactMiddle_Name
Get
Return Me.middle_NameField
End Get
Set(value As ContactMiddle_Name)
Me.middle_NameField = Value
End Set
End Property
'''<remarks/>
Public Property Last_Name() As String
Get
Return Me.last_NameField
End Get
Set(value As String)
Me.last_NameField = Value
End Set
End Property
'''<remarks/>
Public Property TelephoneNumbers() As ContactTelephoneNumbers
Get
Return Me.telephoneNumbersField
End Get
Set(value As ContactTelephoneNumbers)
Me.telephoneNumbersField = Value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ContactMiddle_Name
Private nilField As Boolean
'''<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Qualified, [Namespace]:="w3.org/2001/XMLSchema-instance")> _
Public Property nil() As Boolean
Get
Return Me.nilField
End Get
Set(value As Boolean)
Me.nilField = Value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ContactTelephoneNumbers
Private telephoneNumberField As ContactTelephoneNumbersTelephoneNumber
'''<remarks/>
Public Property TelephoneNumber() As ContactTelephoneNumbersTelephoneNumber
Get
Return Me.telephoneNumberField
End Get
Set(value As ContactTelephoneNumbersTelephoneNumber)
Me.telephoneNumberField = Value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ContactTelephoneNumbersTelephoneNumber
Private numberField As Object
Private isHomeField As Object
'''<remarks/>
Public Property Number() As Object
Get
Return Me.numberField
End Get
Set(value As Object)
Me.numberField = Value
End Set
End Property
'''<remarks/>
Public Property IsHome() As Object
Get
Return Me.isHomeField
End Get
Set(value As Object)
Me.isHomeField = Value
End Set
End Property
End Class
Upon deserialising, the output contains tag which represents an empty element rather then a null. The client wants us to send null rather then empty.

Try giving the properties the following attribute <XmlElement(IsNullable := True)>.
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.isnullable(v=vs.110).aspx

Related

How to use the following model class (Framework 4.7) in a Framework 2.0?

I have the following model class used in my .NET Framework 4.7.2 project which get and set data from an API.
I have to use the same model class in a .NET 2.0 Project which will have to get and set the same object get from a HTTP request (not API)
But if i try just to copy paste it i get the error which says that i need a setter on it.
Here is how my model looks like:
Public Class PDT
Public Class Documento
Public Property testata As Testata
Public Property corpo() As IEnumerable(Of Corpo)
End Class
Public Class Testata
Public Property id As Integer
Public Property cod As String
Public Property tipo As String
Public Property cod_fornitore As String
Public Property desc_fornitore As String
Public Property data As String
Public Property num_pv As String
Public Property desc_pv As String
Public Property num_pv_destinazione As String
Public Property desc_pv_destinazione As String
Public Property num_ordine As String
Public Property inviato As Boolean
End Class
Public Class Corpo
Public Property barcode As String
Public Property desc As String
Public Property um As String
Public Property qta As Single
Public Property id_testata As Integer
Public Property tipo_frontalino As String
Public Property timestamp As Long
End Class
End Class
PS I've tried setting getter and setters like this for each property:
Public Property testata() As Testata
Get
End Get
Set(ByVal value As Testata)
End Set
End Property
But by passing data in that object returns always Nothing.
UPDATE my class with getters and setters:
Public Class PDT
Public Class Documento
Private _testata As Testata = Nothing
Public Property testata As Testata
Get
Return _testata
End Get
Set(ByVal value As Testata)
_testata = value
End Set
End Property
Private _corpo As IEnumerable(Of Corpo) = Nothing
Public Property corpo As IEnumerable(Of Corpo)
Get
Return _corpo
End Get
Set(ByVal value As IEnumerable(Of Corpo))
_corpo = value
End Set
End Property
End Class
Public Class Testata
Private _id As Integer = Nothing
Public Property id As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Private _cod As String = Nothing
Public Property cod As String
Get
Return _cod
End Get
Set(ByVal value As String)
_cod = value
End Set
End Property
Private _tipo As String = Nothing
Public Property tipo As String
Get
Return _tipo
End Get
Set(ByVal value As String)
_tipo = value
End Set
End Property
Private _cod_fornitore As String = Nothing
Public Property cod_fornitore As String
Get
Return _cod_fornitore
End Get
Set(ByVal value As String)
_cod_fornitore = value
End Set
End Property
Private _desc_fornitore As String = Nothing
Public Property desc_fornitore As String
Get
Return _desc_fornitore
End Get
Set(ByVal value As String)
_desc_fornitore = value
End Set
End Property
Private _data As String = Nothing
Public Property data As String
Get
Return _data
End Get
Set(ByVal value As String)
_data = value
End Set
End Property
Private _num_pv As String = Nothing
Public Property num_pv As String
Get
Return _num_pv
End Get
Set(ByVal value As String)
_num_pv = value
End Set
End Property
Private _desc_pv As String = Nothing
Public Property desc_pv As String
Get
Return _desc_pv
End Get
Set(ByVal value As String)
_desc_pv = value
End Set
End Property
Private _num_pv_destinazione As String = Nothing
Public Property num_pv_destinazione As String
Get
Return _num_pv_destinazione
End Get
Set(ByVal value As String)
_num_pv_destinazione = value
End Set
End Property
Private _desc_pv_destinazione As String = Nothing
Public Property desc_pv_destinazione As String
Get
Return _desc_pv_destinazione
End Get
Set(ByVal value As String)
_desc_pv_destinazione = value
End Set
End Property
Private _num_ordine As String = Nothing
Public Property num_ordine As String
Get
Return _num_ordine
End Get
Set(ByVal value As String)
_num_ordine = value
End Set
End Property
Private _inviato As Boolean = Nothing
Public Property inviato As Boolean
Get
Return _inviato
End Get
Set(ByVal value As Boolean)
_inviato = value
End Set
End Property
End Class
Public Class Corpo
Private _barcode As String = Nothing
Public Property barcode As String
Get
Return _barcode
End Get
Set(ByVal value As String)
_barcode = value
End Set
End Property
Private _desc As String = Nothing
Public Property desc As String
Get
Return _desc
End Get
Set(ByVal value As String)
_desc = value
End Set
End Property
Private _um As String = Nothing
Public Property um As String
Get
Return _um
End Get
Set(ByVal value As String)
_um = value
End Set
End Property
Private _qta As Single = Nothing
Public Property qta As Single
Get
Return _qta
End Get
Set(ByVal value As Single)
_qta = value
End Set
End Property
Private _id_testata As Integer = Nothing
Public Property id_testata As Integer
Get
Return _id_testata
End Get
Set(ByVal value As Integer)
_id_testata = value
End Set
End Property
Private _tipo_frontalino As String = Nothing
Public Property tipo_frontalino As String
Get
Return _tipo_frontalino
End Get
Set(ByVal value As String)
_tipo_frontalino = value
End Set
End Property
Private _timestamp As Long = Nothing
Public Property timestamp As Long
Get
Return _timestamp
End Get
Set(ByVal value As Long)
_timestamp = value
End Set
End Property
End Class
End Class
I'm pretty sure VB.NET didn't have auto-properties back then, so you will need to change your code to use the classic properties with a backing field. For example:
' Backing field
Private _cod As String = "Empty"
' Classic property getter and setter
Property cod As String
Get
Return _cod
End Get
Set(ByVal value As String)
_cod = value
End Set
End Property

vb.net No parameterless constructor defined for this object

I am download a Json libary here : http://www.pozzware.com/pozzware/Corsi/Programmazione/VB.NET/JSON%20Library.aspx
This is my class on my project :
Imports PW.JSON
Public Class Prova
Private _id As Integer
Private _name As String
Private _valido As Boolean
Private _subObject As Prova
Private _numero As Integer
Private _numeroDec As Double
Private _array() As String
Public Property ID() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Valido() As Boolean
Get
Return _valido
End Get
Set(ByVal value As Boolean)
_valido = value
End Set
End Property
Public Property SubObject() As Prova
Get
Return _subObject
End Get
Set(ByVal value As Prova)
_subObject = value
End Set
End Property
Public Property NumeroDec() As Double
Get
Return _numeroDec
End Get
Set(ByVal value As Double)
_numeroDec = value
End Set
End Property
Public Property Array() As String()
Get
Return _array
End Get
Set(ByVal value As String())
_array = value
End Set
End Property
Public Sub New(ByVal ID As Integer, ByVal Name As String)
_id = ID
_name = Name
End Sub
Public Function SomeMethod() As String
Return "Method: " & _id
End Function
End Class
And this is my code :
Sub PasteJsonExam()
Dim strJSON As String = "{""NumeroDec"": 100.34, ""Name"": ""Nome Object"", " & _
" ""Array"": [""A"", ""E"", ""I"", ""O"", ""U""], " & _
" ""SubObject"": {""NumeroDec"": 0, ""Name"": ""Nome - SubObject"", " & _
" ""Array"": null, ""SubObject"": null, ""Valido"": false, ""ID"": 2}, " & _
" ""Valido"": true, ""ID"": 1}"
Dim objprova As Prova
objprova = PW.JSON.JSONHelper.StringToObject(strJSON, GetType(Prova))
MsgBox(objprova.Name)
MsgBox(objprova.SubObject.Name)
End Sub
When i call that sub i got error :
No parameterless constructor defined for this object.
At this line :
objprova = PW.JSON.JSONHelper.StringToObject(strJSON, GetType(Prova))
I am not an professonal in VB.net so i hope someone explain for me why i got that error and how can i fix this.
Probably StringToObject method try to create an instance of Prova. It does this by calling the default(parameterless) constructor of the type.
But in the class you defined there isn't any parameterless constructor, you have only:
Public Class Prova
' Other fields and methods
Public Sub New(ByVal ID As Integer, ByVal Name As String)
_id = ID
_name = Name
End Sub
' Other fields and methods
End Class
To let the method work you need to define a parameterless constructor like:
Public Class Prova
' Other fields and methods
Public Sub New()
End Sub
Public Sub New(ByVal ID As Integer, ByVal Name As String)
_id = ID
_name = Name
End Sub
' Other fields and methods
End Class
Well i think that the library you are using is not the best choice, but, if you want to run your program you must add a default constructor (a constructor without parameter). That's because the library probably uses Reflection
This is the class with the Default Constructor:
Imports PW.JSON
Public Class Prova
Private _id As Integer
Private _name As String
Private _valido As Boolean
Private _subObject As Prova
Private _numero As Integer
Private _numeroDec As Double
Private _array() As String
Public Property ID() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Valido() As Boolean
Get
Return _valido
End Get
Set(ByVal value As Boolean)
_valido = value
End Set
End Property
Public Property SubObject() As Prova
Get
Return _subObject
End Get
Set(ByVal value As Prova)
_subObject = value
End Set
End Property
Public Property NumeroDec() As Double
Get
Return _numeroDec
End Get
Set(ByVal value As Double)
_numeroDec = value
End Set
End Property
Public Property Array() As String()
Get
Return _array
End Get
Set(ByVal value As String())
_array = value
End Set
End Property
' This is the default constructor the library should need'
Public Sub New()
End Sub
Public Sub New(ByVal ID As Integer, ByVal Name As String)
_id = ID
_name = Name
End Sub
Public Function SomeMethod() As String
Return "Method: " & _id
End Function
End Class

deserialization error converting to strongly typed object

My object generated by VS2013 'paste as special' fn
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class Contacts
Private deptContactField As ContactsDeptContact
'''<remarks/>
Public Property DeptContact() As ContactsDeptContact
Get
Return Me.deptContactField
End Get
Set(value As ContactsDeptContact)
Me.deptContactField = Value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ContactsDeptContact
Private contactIdField As UInteger
Private descriptionField As String
Private personField As ContactsDeptContactPerson
'''<remarks/>
Public Property ContactId() As UInteger
Get
Return Me.contactIdField
End Get
Set(value As UInteger)
Me.contactIdField = Value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute([Namespace]:="http://schemas.sommething.org/Company.Model")> _
Public Property Description() As String
Get
Return Me.descriptionField
End Get
Set(value As String)
Me.descriptionField = Value
End Set
End Property
'''<remarks/>
Public Property Person() As ContactsDeptContactPerson
Get
Return Me.personField
End Get
Set(value As ContactsDeptContactPerson)
Me.personField = Value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ContactsDeptContactPerson
Private dateOfBirthField As Object
Private firstNameField As String
Private lastNameField As String
Private addressesField As ContactsDeptContactPersonAddresses
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute([Namespace]:="http://schemas.sommething.org/Company.Model")> _
Public Property DateOfBirth() As Object
Get
Return Me.dateOfBirthField
End Get
Set(value As Object)
Me.dateOfBirthField = Value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute([Namespace]:="http://schemas.sommething.org/Company.Model")> _
Public Property FirstName() As String
Get
Return Me.firstNameField
End Get
Set(value As String)
Me.firstNameField = Value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute([Namespace]:="http://schemas.sommething.org/Company.Model")> _
Public Property LastName() As String
Get
Return Me.lastNameField
End Get
Set(value As String)
Me.lastNameField = Value
End Set
End Property
'''<remarks/>
Public Property Addresses() As ContactsDeptContactPersonAddresses
Get
Return Me.addressesField
End Get
Set(value As ContactsDeptContactPersonAddresses)
Me.addressesField = Value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class ContactsDeptContactPersonAddresses
Private personAddressField As PersonAddress
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute([Namespace]:="http://schemas.sommething.org/Company.Model")> _
Public Property PersonAddress() As PersonAddress
Get
Return Me.personAddressField
End Get
Set(value As PersonAddress)
Me.personAddressField = Value
End Set
End Property
End Class
'''<remarks/>
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://schemas.sommething.org/Company.Model"), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://schemas.sommething.org/Company.Model", IsNullable:=False)> _
Partial Public Class PersonAddress
Private addressLine1Field As String
'''<remarks/>
Public Property AddressLine1() As String
Get
Return Me.addressLine1Field
End Get
Set(value As String)
Me.addressLine1Field = Value
End Set
End Property
End Class
Upon deserializing, I get an exception
Message="There is an error in XML document (1, 237)."
InnerException {"Input string was not in a correct format."}
I tried putting XMLAttribute([ElementName]: and that resulted in same exception.
Deserialization from here
Dim p As New Contacts
Dim x As New XmlSerializer(p.GetType)
Dim p2 As New Contacts()
p2 = CType(x.Deserialize(objStreamReader), Contacts)
Trying to obtain a Contacts object and iterate through the all the deptcontact and all of details of the deptcontact.

OData made from objects

Lets say I have a class called "Item" and "SubItems"
Public Class SubItem
Private _SubItemName As String
Public Property SubItemName() As String
Get
Return _SubItemName
End Get
Set(ByVal value As String)
_SubItemName = value
End Set
End Property
End Class
<DataServiceKey("Name")> _
Public Class Item
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _SubItems As List(Of SubItem)
Public Property SubItems() As List(Of SubItem)
Get
Return _SubItems
End Get
Set(ByVal value As List(Of SubItem))
_SubItems = value
End Set
End Property
End Class
How would I create a service that would return a list of Items and upon looking up an individual item I would be able to see the sub items.
You can check out WCF Data Servies reflection provider, e.g.,
http://blogs.msdn.com/b/alexj/archive/2010/06/11/tip-56-writing-an-odata-service-using-the-reflection-provider.aspx
http://msdn.microsoft.com/en-us/library/dd728281.aspx
Hope this helps.

Help me get List<Customers> like following in nhibernate?

I have class following. When i mapping file also following. I only get IList but i have not get List(of OrderTemp).
Help me.
Public Class CusTemp
Private _CustomerID As String
Private _CompanyName As String
Private _ContactName As String
Private _ContactTitle As String
Private _Address As String
Private _City As String
Private _OrderTemp As List(Of OrderTemp)
Public Sub New()
End Sub
Public Property CustomerID() As String
Get
Return _CustomerID
End Get
Set(ByVal value As String)
_CustomerID = value
End Set
End Property
Public Property CompanyName() As String
Get
Return _CompanyName
End Get
Set(ByVal value As String)
_CompanyName = value
End Set
End Property
Public Property ContactName() As String
Get
Return _ContactName
End Get
Set(ByVal value As String)
_ContactName = value
End Set
End Property
Public Property ContactTitle() As String
Get
Return _ContactTitle
End Get
Set(ByVal value As String)
_ContactTitle = value
End Set
End Property
Public Property Address() As String
Get
Return _Address
End Get
Set(ByVal value As String)
_Address = value
End Set
End Property
Public Property City() As String
Get
Return _City
End Get
Set(ByVal value As String)
_City = value
End Set
End Property
Public Property OrderTemp() As List(Of OrderTemp)
Get
Return _OrderTemp
End Get
Set(ByVal value As List(Of OrderTemp))
_OrderTemp = value
End Set
End Property
End Class
mappingfile:
<!--One-to-many mapping: Orders-->
<bag name="OrderTemp" table="Orders" lazy="true">
<key column="CustomerID" />
<one-to-many class="OrderTemp"/>
</bag>
Try using a set mapping
Then in your code you could just do
var criteria = Session.CreateCriteria(typeof(OrderTemp)).List();