Deserializing XML from String in C#

Deserializing XML from String in C#

To deserialize XML from a string in C#, you can use the XmlSerializer class provided by the .NET Framework. Here's an example of how you can do this:

using System.Xml.Serialization;
using System.IO;

// Define a class to represent the data you want to deserialize
[XmlRoot("myData")]
public class MyData
{
    [XmlElement("name")]
    public string Name { get; set; }

    [XmlElement("age")]
    public int Age { get; set; }

    [XmlElement("isMarried")]
    public bool IsMarried { get; set; }
}

// Deserialize the XML string into an object of the MyData class
string xmlString = "<myData><name>John Doe</name><age>30</age><isMarried>true</isMarried></myData>";
XmlSerializer serializer = new XmlSerializer(typeof(MyData));
MyData data;

using (StringReader reader = new StringReader(xmlString))
{
    data = (MyData)serializer.Deserialize(reader);
}

// Use the deserialized object
Console.WriteLine($"Name: {data.Name}, Age: {data.Age}, Married: {data.IsMarried}");

In this example, we define a MyData class that represents the data we want to deserialize from the XML string. We use the [XmlRoot] attribute to specify the root element name of the XML, and the [XmlElement] attribute to specify the names of the child elements that correspond to the properties of the class.

We then create an instance of the XmlSerializer class for the MyData type, and use the Deserialize method to deserialize the XML string into an object of the MyData class. We wrap the StringReader object in a using block to ensure that it is properly disposed of after use.

Finally, we use the deserialized MyData object to display the values of its properties.

Note that the XML string must be well-formed and conform to the structure of the MyData class, or an exception will be thrown during deserialization.

Examples

  1. "C# deserialize XML string using XmlSerializer"

    Code:

    var xmlString = "<Root><Element>Value</Element></Root>";
    
    var serializer = new XmlSerializer(typeof(MyClass));
    using (var reader = new StringReader(xmlString))
    {
        var obj = (MyClass)serializer.Deserialize(reader);
    }
    

    Description: Deserialize an XML string into an object (MyClass) using XmlSerializer and StringReader.

  2. "C# deserialize XML string using XElement"

    Code:

    var xmlString = "<Root><Element>Value</Element></Root>";
    
    var element = XElement.Parse(xmlString);
    var obj = new MyClass
    {
        Property = element.Element("Element").Value
    };
    

    Description: Use XElement.Parse to parse an XML string and extract values directly into properties of an object (MyClass).

  3. "C# deserialize XML string using XmlReader"

    Code:

    var xmlString = "<Root><Element>Value</Element></Root>";
    
    var serializer = new XmlSerializer(typeof(MyClass));
    using (var reader = XmlReader.Create(new StringReader(xmlString)))
    {
        var obj = (MyClass)serializer.Deserialize(reader);
    }
    

    Description: Deserialize an XML string into an object (MyClass) using XmlSerializer and XmlReader with a StringReader.

  4. "C# deserialize XML string with anonymous type"

    Code:

    var xmlString = "<Root><Element>Value</Element></Root>";
    
    var obj = new { Element = XElement.Parse(xmlString).Element("Element").Value };
    

    Description: Deserialize specific elements from an XML string into an anonymous type using XElement.Parse.

  5. "C# deserialize XML string using DataContractSerializer"

    Code:

    var xmlString = "<Root><Element>Value</Element></Root>";
    
    var serializer = new DataContractSerializer(typeof(MyClass));
    using (var reader = new StringReader(xmlString))
    {
        var obj = (MyClass)serializer.ReadObject(XmlReader.Create(reader));
    }
    

    Description: Utilize DataContractSerializer to deserialize an XML string into an object (MyClass) with a StringReader and XmlReader.

  6. "C# deserialize XML string to dynamic object"

    Code:

    var xmlString = "<Root><Element>Value</Element></Root>";
    
    dynamic obj = XElement.Parse(xmlString);
    

    Description: Deserialize an XML string into a dynamic object using XElement.Parse, providing flexibility for handling various XML structures.

  7. "C# deserialize XML string using System.Text.Json"

    Code:

    var xmlString = "<Root><Element>Value</Element></Root>";
    
    var options = new JsonSerializerOptions
    {
        Converters = { new XmlConverter() }
    };
    
    var obj = JsonSerializer.Deserialize<MyClass>(xmlString, options);
    

    Description: Use System.Text.Json with a custom XmlConverter to deserialize an XML string into an object (MyClass).

  8. "C# deserialize XML string using LINQ to XML"

    Code:

    var xmlString = "<Root><Element>Value</Element></Root>";
    
    var obj = new MyClass
    {
        Property = XElement.Parse(xmlString).Element("Element").Value
    };
    

    Description: Leverage LINQ to XML for a concise way to parse and extract values from an XML string into an object (MyClass).

  9. "C# deserialize XML string with multiple elements"

    Code:

    var xmlString = "<Root><Element1>Value1</Element1><Element2>Value2</Element2></Root>";
    
    var obj = new MyClass
    {
        Property1 = XElement.Parse(xmlString).Element("Element1").Value,
        Property2 = XElement.Parse(xmlString).Element("Element2").Value
    };
    

    Description: Parse and extract values from multiple elements in an XML string into corresponding properties of an object (MyClass).

  10. "C# deserialize XML string with XmlRoot attribute"

    Code:

    [XmlRoot("Root")]
    public class MyClass
    {
        [XmlElement("Element")]
        public string Property { get; set; }
    }
    
    var xmlString = "<Root><Element>Value</Element></Root>";
    
    var serializer = new XmlSerializer(typeof(MyClass));
    using (var reader = new StringReader(xmlString))
    {
        var obj = (MyClass)serializer.Deserialize(reader);
    }
    

    Description: Use XmlRoot and XmlElement attributes to specify the root element and element names during XML deserialization into an object (MyClass).


More Tags

redisjson aws-appsync abap accord.net pyodbc integration tensorflow-datasets google-authenticator semaphore zxing

More C# Questions

More Bio laboratory Calculators

More Math Calculators

More Geometry Calculators

More Internet Calculators