Answer the question
In order to leave comments, you need to log in
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);
}
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question