Archive

Posts Tagged ‘XML’

XML Serialization


   Private Function LoadInvoice(XMLFilename As String) As Boolean
      Try

         Dim reader As TextReader = New StreamReader(XMLFilename)
         Dim serializer As New XmlSerializer(GetType(Invoice.Documents))

         Dim myInvoices As New Invoice.Documents
         myInvoices = DirectCast(serializer.Deserialize(reader), Invoice.Documents)
         reader.Close()

         Dim xmlOutputFilename As String = Path.GetDirectoryName(XMLFilename)
         xmlOutputFilename &= "\" & Path.GetFileNameWithoutExtension(XMLFilename) & "_1.xml"

         Dim writer As FileStream = New FileStream(xmlOutputFilename, FileMode.Create)
         serializer = New XmlSerializer(GetType(Invoice.Documents))

         serializer.Serialize(writer, myInvoices)
         writer.Close()


      Catch ex As Exception
         Log.TraceError(ex, String.Format("Error occured in {0}", System.Reflection.MethodInfo.GetCurrentMethod.Name))

      End Try
   End Function

Categories: XML Serialization Tags: ,

Reading XML into Generic List of Classes

November 18, 2010 Leave a comment

Public Function GetCustomers() As List(Of Customer)
Dim data As XDocument = XDocument.Load("c:\customers\customers.xml"))

Return (From c In data.Descendants("Customer")
Order By c.Attribute("Surname")
New Customer() With { _
Key .ID = Convert.ToInt32(c.Attribute("ID").Value), _
Key .Forename = c.Element("Forename").Value, _
Key .Surname = c.Element("Surname").Value, _
Key .DOB = c.Element("DOB").Value, _
Key .Location = c.Element("Location").Value _
}).ToList()
End Function

 

Categories: Generics, LINQ Tags: ,

Generic List of Classes to XML using LINQ

November 18, 2010 Leave a comment

Dim _doc As New XDocument(New XDeclaration("1.0", "UTF-8", "yes"), _
 New XElement("EmailAccounts", _
 From EMailAccount In CEmailAccountsList _
 Select New XElement("EMailAccount", _
 New XElement("AccountFromSender", EMailAccount.AccountFromSender), _
 New XElement("EMailAddress", EMailAccount.EMailAddress), _
 New XElement("MailServerAddress", EMailAccount.MailServerAddress), _
 New XElement("MailServerType", EMailAccount.MailServerType), _
 New XElement("PaperFlowImportDirectory", EMailAccount.PFImportDirectory), _
 New XElement("Password", EMailAccount.Password), _
 New XElement("Port", EMailAccount.Port), _
 New XElement("PropertyID", EMailAccount.PropertyID), _
 New XElement("PropertyIDFromSubject", EMailAccount.PropertyIDFromSubject), _
 New XElement("SecurityType", EMailAccount.SecurityType), _
 New XElement("SubjectAsIndex", EMailAccount.SubjectAsIndex), _
 New XElement("Username", EMailAccount.Username))))

 _doc.Save(constEMailSettingsFilename)

Categories: Generics, LINQ Tags: ,