M
M
Mikhail Mikhail2017-08-12 14:20:06
In contact with
Mikhail Mikhail, 2017-08-12 14:20:06

How to serialize vk friends list using vknet?

Good afternoon. I pick C# and VkNet. At the moment, I want to: save the list of friends to disk, and the next time I open the application, load a fresh list of friends and compare with what is written on the disk!
But at me that that not absolutely turns out to make the given machination "beautifully". but I don’t want to do it with crutches)

namespace VkApi
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string> code = () =>
            {
                Console.Write("Please enter code: ");
                string value = Console.ReadLine();
 
                return value;
            };
 
            string login = "*********";
            string pass = "**********";
            Settings scope = Settings.All;
 
 
            var vk = new VkNet.VkApi();
            vk.Authorize(new ApiAuthParams
            {
                ApplicationId = ********,
                Login = login,
                Password = pass,
                Settings = scope
//                TwoFactorAuthorization = code
            });
            var records = vk.Audio.Get(vk.UserId.Value);
            Console.WriteLine("Records count: " + records.Count);
            var friends = vk.Friends.Get(vk.UserId.Value, ProfileFields.FirstName | ProfileFields.LastName);
//            foreach (var friend in friends)
//            {
//                Console.WriteLine(friend.LastName+" "+friend.FirstName);
//                
//            }
            XmlSerializer ser = new XmlSerializer(typeof(FriendList));
            string path = Path.GetRandomFileName();
            FileStream file = new FileStream(path,FileMode.Create,FileAccess.Write,FileShare.None);
            ser.Serialize(file,friends);
            Console.WriteLine("type friends = " + friends.ToString());
 
//            Console.WriteLine("count friends = "+friends.Count);
        }
    }
}

currently swearing at
spoiler

Unhandled Exception: System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidCastException: Unable to cast object of type 'System.Collections.ObjectModel.ReadOnlyCollection`1[VkNet.Model.User]' to type 'VkNet.Model.FriendLi
st'.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterFriendList.Write3_FriendList(Object o)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o, XmlSerializerNamespaces namespaces)
at VkApi.Program.Main(String[] args) in D:\project\NetCore\VkApi\Program.cs:line 55

Tell me how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kuznetsov, 2017-08-12
@DarkRaven

It swears, because you tell the serializer that you give it a FriendList, and you yourself serialize the VkNet.Model.User collection.
You need to make, for example, a List list, fill it from friends, then serialize it.
Well, XmlSerializer(typeof(FriendList)); replace with XmlSerializer(typeof(List));
UPD. Snippet from LinqPad

public class UserDTO
{
  public string Text { get; set; }
}

void Main()
{
  var listOfUsers = new List<UserDTO>
  {
    new UserDTO { Text =  "VkUserName" }
  };
  var ser = new System.Xml.Serialization.XmlSerializer(typeof(List<UserDTO>));
  var sb = new StringBuilder();
  var wri = new StringWriter(sb);
  ser.Serialize(wri, listOfUsers);
  sb.ToString().Dump();
}

// Define other methods and classes here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question