Tuesday, January 4, 2011
Nausea Dizziness Sore Throat
Wikipedia
:
Imports
System.Xml.Serialization
Public Class Invoice
Public Sub New
() & #
- List (Of DettaglioFattura
- )
- & # 160; End Sub
- Public Property DataEmissione As DateTime
- Public Property Customer As Customer
- ; Public Property
- NumeroDocumento
- As String Public Property Details As
- List (Of DettaglioFattura ) Public
- Property Stato If StatoFattura
- Public ReadOnly Property Total If Decimal Get
- & # 160; If Dettagli Is Nothing Then
-                  Return 0              Else                  Return Dettagli.Sum( Function
- (d) d .TotaleIvato)
-              End If          End Get
-      End
- Property
- End Class
- Public
- Class DettaglioFattura
- Public Property Description As String
- As String
- Public Property
- Quantita As
- Integer      Public Property PrezzoUnitario As
- Decimal      Public Property Iva As
- Decimal      Public ReadOnly
- Property Totale As Decimal          Get
-              Return Quantita * PrezzoUnitario          End Get
-      End Property      Public ReadOnly Property
- TotaleIvato If
- Decimal
- Get
- Total Return * (1 + Iva) End Get
-      End Property End Class
-  
- Public Class
- Cliente
-      Public Property Denominazione As
    
Public- As
- String      Public Property
- PartitaIVA As
- String End Class
-  
- Public Enum StatoFattura
-     DaPagare
-     Pagata     Annullata End Enum
- XML Serialization in The first type of XML serialization is that we will see, that we will see how to "write" the instances of our class invoice formats XML. order to serialize our class in an XML format, the same class must be serializable, which is composed of properties (the methods are not serialized) serializable. If, for example, our class has a property of a type. NET does not serialize the entire class is not serializable. The serialization process is based on the use della classe XmlSerializer e di un oggetto che serva da flusso in cui scrivere l’XML risultante (un XmlWriter, un TextWriter, uno Stream).
- In particolare il costruttore dell’XmlSerializer prevede che venga dichiarata per quale classe stiamo costruendo il serializzatore, quindi possiamo utilizzare il metodo Serialize() per scrivere l’XML risultatnte nello stream o nel writer opportuno. La seguente funzione restituisce la stringa XML utilizzando uno StringWriter:
- Public Shared Function
Fattura
)
AsString
    
Iffattura
Is- Then
- Throw New ArgumentNullException ( "Fattura" )      Dim strXml As String = Nothing
-      Dim writer As New XmlSerializer ( GetType ( Fattura ))      Using
- strWriter As New StringWriter ()          writer
- .Serialize(strWriter, fattura)         strXml = strWriter .ToString()      End Using      Return
- strXml End Function
- La seguente figura mostra la serializzazione di una fattura di prova:
- Come possiamo osservare, tutte le proprietà della nostra classe vengono riportate all’interno dell’XML frutto della serializzazione. Ogni proprietà diventa un tag XML con il nome pari al nome della proprietà. Infine lo stato della fattura (definito come un’enumerazione) viene scritto come una stringa esattamente pari al valore dell’enumerazione impostato. A livello di codice, nel momento in cui viene eseguito il Serialize, vengono richiamati tutti Get the properties of read / write class (if not above the total property). In fact, the behavior seen earlier, is the default, but we can intervene to change the result of the XML and we can do it using the appropriate attributes in the namespace System.Xml.Serialization copntenuti. If we, for example, that one or more properties of our class do not fall within the XML, we can use the attribute XmlIgnoreAttribute decorating their property (ol'attributo) that do not want to end up in XML .
- For example, suppose you have una proprietà della nostra fattura, chiamata Id, che non vogliamo serializzare. Potremo scrivere:
- < XmlIgnore ()>
Property
Id As
Integer ?
In questo modo l’XML che si ottiene non ha il tag <Id>.
In maniera analoga, se vogliamo che una nostra proprietà non diventi un tag XML ma un attributo del tag Fattura, possiamo utilizzare l’attributo XmlAttribute indicando, eventualmente, il nome dell’attributo (altrimenti viene utilizzato il nome della properties):
- \u0026lt; XmlAttribute ()>
- Public Property NumeroDocumento As String
And if we wanted an XML tag has the name property but a different name, then we could use the XmlElement attribute indicamndo the tag name:
- XmlElement ( "Issue" )>
- Public Property DataEmissione As DateTime
attributes contained in the namespace System.Serialization allow us, therefore, to change the result of serialization from the default.
- The XmlSerializer allows us to deserialize a string XML into an object. In this case, instead of a writer such as support, we must use a reader:
- Public Shared Function DeserializzaXML (ByVal strFattura As
Invoice
Dim
retObj
As- Dim reader = New XmlSerializer (GetType ( Fattura ))      Dim strReader = New
- StringReader (strFattura)      Try         retObj = CType
- ( reader .Deserialize(strReader), Fattura )      Catch ex As Exception
-          Throw
- End Try Return
- retObj End Function
- Obviously, if the XML is not adhering to the scheme, we get an exception when deserialized.
- But what happens when the deserialization is done? When Deserialize method is called, the class object is instantiated Fattiura (constructor is called) and, therefore, are invoked in sequence sets of properties found in the XML.
- For this reason, if we do not place a tag inside the XML file serialized, the resulting property will contain the default value. Attention, therefore, that the properties have a default value consistently. In the next post we will briefly SOAP serialization and, above all, the JSON serialization. Stay Tuned!!
- Technorati tags:
,
xml
0 comments:
Post a Comment