public static DeSerializeResult fromXml <T>(this T value, string xml, DeSerializeConfig config = null) { return(XmlDeSerializer.DeSerialize(xml, ref value, config)); }
string xmlString = ""; var serializer = new XmlSerializer(typeof(Person)); using (TextReader reader = new StringReader(xmlString)) { Person person = (Person)serializer.Deserialize(reader); Console.WriteLine(person.Name); // Output: John Console.WriteLine(person.Age); // Output: 30 } John 30
[XmlRoot("book")] public class Book { [XmlAttribute("id")] public string Id { get; set; } [XmlElement("title")] public string Title { get; set; } [XmlElement("author")] public string Author { get; set; } } string xmlString = "In this example, we are deserializing an XML string into a Book object. We use XmlAttributes to specify how the serialization should be performed. The XmlRoot attribute specifies that the root element should be named "book". The XmlAttribute attribute specifies that the "id" attribute of the "book" element should be mapped to the Id property of the Book object. Finally, the XmlElement attribute specifies that the "title" and "author" elements should be mapped to the Title and Author properties, respectively. The package library for the XmlSerializer class is System.Xml.Serialization."; var serializer = new XmlSerializer(typeof(Book)); using (TextReader reader = new StringReader(xmlString)) { Book book = (Book)serializer.Deserialize(reader); Console.WriteLine(book.Title); // Output: The Great Gatsby Console.WriteLine(book.Author); // Output: F. Scott Fitzgerald Console.WriteLine(book.Id); // Output: 1 } The Great Gatsby F. Scott Fitzgerald
public static DeSerializeResult fromXml <T>(this T value, string xml, DeSerializeConfig config = null) { return(XmlDeSerializer.DeSerialize(xml, ref value, config)); }