How to Deserialize XML document in C#

How to Deserialize XML document in C#

To deserialize an XML document in C#, you can use the XmlSerializer class, which is located in the System.Xml.Serialization namespace. Here's an example:

Suppose you have the following XML document:

 <?xml version="1.0" encoding="UTF-8"?> <Books> <Book> <Title>The Catcher in the Rye</Title> <Author>J.D. Salinger</Author> <Year>1951</Year> </Book> <Book> <Title>To Kill a Mockingbird</Title> <Author>Harper Lee</Author> <Year>1960</Year> </Book> </Books> 

You can create a corresponding C# class to represent the structure of the XML document, like this:

 [XmlRoot("Books")] public class Bookstore { [XmlElement("Book")] public List<Book> Books { get; set; } } public class Book { public string Title { get; set; } public string Author { get; set; } public int Year { get; set; } } 

Then, to deserialize the XML document into an instance of the Bookstore class, you can use the following code:

 string xmlString = File.ReadAllText("path/to/xml/file.xml"); XmlSerializer serializer = new XmlSerializer(typeof(Bookstore)); using (TextReader reader = new StringReader(xmlString)) { Bookstore result = (Bookstore)serializer.Deserialize(reader); } 

In this example, the XmlSerializer is instantiated with the type of the Bookstore class, and a TextReader is created from the XML document string using a StringReader. The Deserialize method is then called on the serializer, passing in the TextReader, which returns an instance of the Bookstore class.

Note that you'll need to add a reference to the System.Xml assembly to use the XmlSerializer class. Also, make sure that the XML document and the C# class have matching structure and naming conventions, or specify the appropriate attributes on the C# class properties to map them to the correct XML element or attribute names.

Examples

  1. "C# deserialize XML to object using XmlSerializer"

    Code:

    var xmlString = "<YourRootElement><Property1>Value1</Property1></YourRootElement>"; var serializer = new XmlSerializer(typeof(YourObjectType)); using (var reader = new StringReader(xmlString)) { var result = (YourObjectType)serializer.Deserialize(reader); } 

    Description: Demonstrates using XmlSerializer to deserialize an XML string into a C# object.

  2. "C# XML deserialization with XmlReader"

    Code:

    var xmlString = "<YourRootElement><Property1>Value1</Property1></YourRootElement>"; using (var reader = XmlReader.Create(new StringReader(xmlString))) { var serializer = new XmlSerializer(typeof(YourObjectType)); var result = (YourObjectType)serializer.Deserialize(reader); } 

    Description: Illustrates deserialization using XmlReader with XmlSerializer for greater control over the process.

  3. "How to deserialize XML to anonymous type in C#"

    Code:

    var xmlString = "<YourRootElement><Property1>Value1</Property1></YourRootElement>"; var serializer = new XmlSerializer(new { Property1 = "" }.GetType()); using (var reader = new StringReader(xmlString)) { var result = serializer.Deserialize(reader); } 

    Description: Shows how to use XmlSerializer for deserializing XML into an anonymous type with a predefined structure.

  4. "C# XML deserialization with XElement"

    Code:

    var xmlString = "<YourRootElement><Property1>Value1</Property1></YourRootElement>"; var element = XElement.Parse(xmlString); var result = new XmlSerializer(typeof(YourObjectType)).Deserialize(element.CreateReader()) as YourObjectType; 

    Description: Introduces deserialization using XElement along with XmlSerializer for XML documents.

  5. "C# XML deserialization with DataContractSerializer"

    Code:

    var xmlString = "<YourRootElement><Property1>Value1</Property1></YourRootElement>"; using (var reader = new StringReader(xmlString)) { var serializer = new DataContractSerializer(typeof(YourObjectType)); var result = serializer.ReadObject(XmlReader.Create(reader)) as YourObjectType; } 

    Description: Demonstrates deserialization using DataContractSerializer for scenarios where DataContract attributes are used.

  6. "C# XML deserialization with XmlNodeReader"

    Code:

    var xmlString = "<YourRootElement><Property1>Value1</Property1></YourRootElement>"; var document = new XmlDocument(); document.LoadXml(xmlString); using (var reader = new XmlNodeReader(document)) { var serializer = new XmlSerializer(typeof(YourObjectType)); var result = serializer.Deserialize(reader) as YourObjectType; } 

    Description: Shows how to use XmlNodeReader along with XmlSerializer for deserialization from an XmlDocument.

  7. "How to deserialize XML with multiple namespaces in C#"

    Code:

    var xmlString = "<ns1:YourRootElement xmlns:ns1='Namespace1' xmlns:ns2='Namespace2'><ns1:Property1>Value1</ns1:Property1></ns1:YourRootElement>"; var serializer = new XmlSerializer(typeof(YourObjectType), "Namespace1"); using (var reader = new StringReader(xmlString)) { var result = serializer.Deserialize(reader); } 

    Description: Illustrates deserialization of XML with multiple namespaces using XmlSerializer with the specified namespace.

  8. "C# XML deserialization with XmlReaderSettings"

    Code:

    var xmlString = "<YourRootElement><Property1>Value1</Property1></YourRootElement>"; var serializer = new XmlSerializer(typeof(YourObjectType)); var settings = new XmlReaderSettings { IgnoreWhitespace = true }; using (var reader = XmlReader.Create(new StringReader(xmlString), settings)) { var result = (YourObjectType)serializer.Deserialize(reader); } 

    Description: Demonstrates deserialization using XmlReaderSettings for customizing the XmlReader behavior.

  9. "C# XML deserialization with XmlElementAttribute"

    Code:

    [XmlElement("CustomPropertyName")] public string Property1 { get; set; } 

    Description: Introduces the usage of XmlElementAttribute to specify a custom property name during XML deserialization.

  10. "C# XML deserialization with XmlArrayAttribute"

    Code:

    [XmlArray("Items")] [XmlArrayItem("Item")] public List<string> Property1 { get; set; } 

    Description: Demonstrates the use of XmlArrayAttribute and XmlArrayItemAttribute for deserializing XML arrays into a C# list property.


More Tags

http2 tasklist db2-400 set-returning-functions string-comparison linechart angular-ivy bson onsubmit ag-grid

More C# Questions

More Electrochemistry Calculators

More Various Measurements Units Calculators

More Physical chemistry Calculators

More Bio laboratory Calculators