publicclassPersonalInformation { publicstring? Name { get; set; } publicint Age { get; set; } public Gender Sex { get; set; } }
publicclassSerializeLearning { publicstaticvoidMain() { var personalInformation = new PersonalInformation { Name = "MaP1e", Age = 18, Sex = Gender.male, };
publicclassSerializeLearning { publicstaticasync Task Main() { using HttpClient client = new() { BaseAddress = new Uri("http://jsonplaceholder.typicode.com") };
// Get the comment information. Comment? comment = await client.GetFromJsonAsync<Comment>("comments/1"); Console.WriteLine($"PostId: {comment?.PostId}"); Console.WriteLine($"Id: {comment?.Id}"); Console.WriteLine($"Name: {comment?.Name}"); Console.WriteLine($"Email: {comment?.Email}"); Console.WriteLine($"Body: {comment?.Body}");
// Post a new comment. HttpResponseMessage response = await client.PostAsJsonAsync("comments", comment); Console.WriteLine($"{(response.IsSuccessStatusCode ? "Success" : "Error")} - {response.StatusCode}"); } }
// 控制台输出 // // PostId: 1 // Id: 1 // Name: id labore ex et quam laborum // Email: Eliseo@gardner.biz // Body: laudantium enim quasi est quidem magnam voluptate ipsam eos // tempora quo necessitatibus // dolor quam autem quasi // reiciendis et nam sapiente accusantium // Success - Created
MySerializableClass myObject = new MySerializableClass();
// Insert code to set properties and fields of the object. XmlSerializer mySerializer = new XmlSerializer(typeof(MySerializableClass));
// To write to a file, create a StreamWriter object. StreamWriter myWriter = new StreamWriter("myFileName.xml"); mySerializer.Serialize(myWriter, myObject); myWriter.Close();
// Construct an instance of the XmlSerializer with the type // of object that is being deserialized. var mySerializer = new XmlSerializer(typeof(MySerializableClass));
// To read the file, create a FileStream. usingvar myFileStream = new FileStream("myFileName.xml", FileMode.Open);
// Call the Deserialize method and cast to the object type. var myObject = (MySerializableClass)mySerializer.Deserialize(myFileStream);