1

So, I have a C# project in which, I am loading a XML document (contains name of students and Id) using Linq to xml and I have to get the associated data (their due date, amount and stuff ) from a WCF service. I added the service with just right click and add service reference and now need to pass arrays to the GetData function, which I initialized but its null obviously. I cant able to convert my array to service type and the function returns array too. How do I assign the array to studentArray ?

 ServiceReference1.ServiceClient client = new ServiceReference1.RycorServiceClient();

Application.ServiceReference1.Student[] studentArray = new ServiceReference1.Student[9];

        Student[] array = studentList.ToArray();

        //for (int i = 0; i <= array.Count(); i++)
        //    studentArray[i] = (RycorApplication.ServiceReference1.Student)array[i];

        //this gives me an error says Cannot convert type 'Application.Student' to 'Application.ServiceReference1.Student'.

        var data = client.GetData(studentArray);

After getting this data, how do I save this data to my XML file ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397

2 Answers2

1

You are getting this error because Application.Student is a different type, you can try to use Application.ServiceReference.Student to save the list of students instead of the "studentList" type.

I suppose that "studentList is an "Application.Student" list and you have to use the same model or make a copy between them using something like this (in the first answer): Copy values from one object to another

Community
  • 1
  • 1
zapico
  • 2,396
  • 1
  • 21
  • 45
  • List studentList is a list of values i loaded from the xml file, in the Student class I created. –  Feb 17 '13 at 04:27
  • studentArray is empty, i need to pass array of students in getdata funtion in order to get data but student Array is empty rightnow –  Feb 17 '13 at 04:30
  • The problem is not the content (it's empty). The problem is its type. Try to do it with Application.ServiceReference1.Student[] array = new ServiceReference1.Student[9]; – zapico Feb 17 '13 at 04:51
  • thats what i am doing, array is Application.ServiceRefrence1.Student[] type, same as studentArray in my code but what type should be array type in my code. –  Feb 17 '13 at 05:05
  • No it isn't. Look at array declaration: Student[] array = studentList.ToArray(); This is a different type than Application.ServiceReference1.Student[]. – zapico Feb 17 '13 at 16:48
0

You pretty much have to do this:

List<ServiceReference1.Student> wcfStudentList = new System.Collections.Generic.List<ServiceReference1.Student>();
        foreach (var student in studentList)
        {
            wcfStudentList.Add(new ServiceReference1.Student()
            {
                ID = student.ID,
                Name = student.Name,
                ..etc..
            });
        }
        var data = client.GetStudentData(wcfStudentList.ToArray());

I do have to question why you don't just change the WCF call if you can to take a List of the student IDs instead of passing the entire object though?

Mike C.
  • 3,024
  • 2
  • 21
  • 18
  • because, the project i doing, they already gave the service, i did not created the wcf service. –  Feb 17 '13 at 05:13
  • studentArray is Empty, its just empty array of 9. –  Feb 17 '13 at 21:01
  • Edited for less confusion (hopefully). Basically, the only way I know here is to iterate over the array of students that you already have and convert each object into the type that the wcf service is expecting. – Mike C. Feb 17 '13 at 21:06