Archive

Archive for the ‘LINQ’ Category

Dynamicaly create XML Elements in XML Literals

February 6, 2011 Leave a comment

XML Literals

d is a Class that has a Fuction named GetXMLFilenames defined.
GetXMLFilenames returns a List(Of XElements).


Dim _XMLDoc = <OBJECT>
  <OBJTYPE>0</OBJTYPE>
  <PAGES><%= d.PageCount %></PAGES>
  <%= From elem In d.GetXMLFilenames Select
     New XElement(elem.Name, elem.Value)
  %>
</OBJECT>
  
   Public Function GetXMLFilenames() As List(Of XElement)
      Try
         Dim tempstr As String = ""
         Dim _XElementList As New List(Of XElement)

         Dim _i As Integer = 0
         For Each _Filename In _Filenames
            _i += 1
            Dim _XElement As New XElement("FILE" &amp; _i, _Filename)
            _XElementList.Add(_XElement)
         Next
         Return _XElementList

      Catch ex As Exception
         Throw ex
      End Try
   End Function

Categories: LINQ, VB.NET 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: ,