Как сериализовать список друзей ? - C#
Формулировка задачи:
- 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);
- }
- }
- }
Не по теме:
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
Решение задачи: «Как сериализовать список друзей ?»
- public class VkUser
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public long ID { get; set; }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д