Home > Backend Development > C++ > How to Deserialize XML Documents into C# Objects?

How to Deserialize XML Documents into C# Objects?

Barbara Streisand
Release: 2025-02-02 17:36:10
Original
780 people have browsed it

How to Deserialize XML Documents into C# Objects?

In the environment of the xml document under the environment of the xml document environment, detailed explanation of

When processing XML documents in the .NET application, the XML document is often serialized into a C#object. This process involves converting XML data to the corresponding C#representation form.

Example XML document

Considering the following XML documents containing various car information:

<?xml version="1.0" encoding="utf-8"?><cars><car><stocknumber>1020</stocknumber><make>Nissan</make><model>Sentra</model></car><car><stocknumber>1010</stocknumber><make>Toyota</make><model>Corolla</model></car><car><stocknumber>1111</stocknumber><make>Honda</make><model>Accord</model></car></cars>
Copy after login
Definition C#

In order to deepen the XML document, we need to define the C#class that represents the XML structure. Create two categories: <类>

    CAR class: Details of a single car.
  •   [Serializable()]
      public class Car
      {
        [System.Xml.Serialization.XmlElementAttribute("StockNumber")]
        public string StockNumber{ get; set; }
    
        [System.Xml.Serialization.XmlElementAttribute("Make")]
        public string Make{ get; set; }
    
        [System.Xml.Serialization.XmlElementAttribute("Model")]
        public string Model{ get; set; }
      }
    Copy after login
  • Cars: The root element of the XML document contains an array of a CAR object.
  •   [System.Xml.Serialization.XmlRootAttribute("Cars", Namespace = "", IsNullable = false)]
      public class Cars
      {
        [XmlArrayItem(typeof(Car))]
        public Car[] Car { get; set; }
    
      }
    Copy after login
  • The process of deepening serialization

To deserture the XML document, please create a Carserializer class containing the Deserialize method:

This method loads the XML document, uses XMLSerializer to convert it to a CARS object, and returns a counter serialized data.
public class CarSerializer
{
    public Cars Deserialize()
    {
        string path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data/") + "cars.xml";

        using (XmlReader reader = XmlReader.Create(path))
        {
            XmlSerializer ser = new XmlSerializer(typeof(Cars));
            Cars cars = (Cars)ser.Deserialize(reader);
            return cars;
        }
    }
}
Copy after login

Using the data of the carrier -oriented data

Now, you can use the Deseerialize method to obtain the CARS object of the sequentialized, and then access it to operate and retrieve information about the car.

The above is the detailed content of How to Deserialize XML Documents into C# Objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template