Friday, 22 March 2013

convert list to xml asp.net VB.NET using LINQ

convert list to xml asp.net VB.NET using LINQ

This example explains how to convert list type to XML using LINQ .

Create a class called Student

  Class Student
        Public Name As String,Grade As String
        Public year As Integer
  End Class

Add List items of type Student

             Dim stuList As List<Student> =  New List<Student>()
            stuList.Add(New Student
            {
                 Name="Shyam", Grade="A",year=1990
            }
)
            stuList.Add(New Student
            {
                 Name = "Raghu", Grade = "B", year = 2010
            }
)
            stuList.Add(New Student
            {
                 Name = "Paul", Grade = "C", year = 2001
            }
)
            stuList.Add(New Student
            {
                 Name = "murthy", Grade = "D", year = 2010
            }
)
            stuList.Add(New Student
            {
                 Name = "madam", Grade = "E", year = 2000
            }
)


Create XML from List

Dim root As New XElement("root")

For i As Integer = 0 To stuList.Count - 1
    Dim ele As New XElement("Student", New XElement("Name", stuList(i).Name), New XElement("Grade", stuList(i).Grade), New XElement("Year", stuList(i).year))
    root.Add(ele)
Next
Console.WriteLine(root)
OUTPUT:
<root>
  <Student>
    <Name>Shyam</Name>
    <Grade>A</Grade>
    <Year>1990</Year>
  </Student>
  <Student>
    <Name>Raghu</Name>
    <Grade>B</Grade>
    <Year>2010</Year>
  </Student>
  <Student>
    <Name>Paul</Name>
    <Grade>C</Grade>
    <Year>2001</Year>
  </Student>
  <Student>
    <Name>murthy</Name>
    <Grade>D</Grade>
    <Year>2010</Year>
  </Student>
  <Student>
    <Name>madam</Name>
    <Grade>E</Grade>
    <Year>2000</Year>
  </Student>
</root>
Tags:convert list to xml asp.net VB.NET using LINQ,convert list to xml asp.neT,convert list to xml VB.NET,LINQ to XML XElement usage.

1 comment:

  1. VBConversions - Convert VB to C# with the best conversion tool on the market.Convert VB to C# with VBConversions,the most accurate code translation tool available.
    any information then visit: www.vbconversions.com and Product Details : www.tangiblesoftwaresolutions.com/Product_Details/Instant_CSharp.html

    vb.net to c#

    ReplyDelete