Search code examples
c#xmlvisual-studioxmlserializer

How to Deserialize XML Attributes into objects, then iterate through the objects


I would like to be able to 'do things' to the Vehicles in the XML file. Ideally I want to iterate through all the vehicles and do a calculation on their price, and change whether they are OnSale. These values would then be displayed in the UI. My code deserializes the XML file but I am unable to access any of the attributes for the Vehicle. I do not need to serialize the objects back to XML.

I have tried to Console.WriteLine the Price, however when I run the code it is returned as 0. Should I be creating an Array of ResponseGeographyVendorRegionVehicle? and then somehow adding objects of that type to the array?

This is the XML File:

<?xml version="1.0" encoding="utf-8" ?>
<Response>
  <Geography>
    <Vendor id="JOHN">
      <Region id="1"></Region>
      <Region id="2">
        <Vehicle Make="HONDA" Fuel="Gas" Price="12000" OnSale="Y" Account="JOHNH" />
        <Vehicle Make="ACURA" Fuel="Gas" Price="14100" OnSale="Y" Account="JOHNH" />
        <Vehicle Make="TOYOTA" Fuel="Gas" Price="8000" OnSale="N" Account="JOHNH" />
        <Vehicle Make="HYUNDAI" Fuel="Gas" Price="13000" OnSale="Y" Account="JOHNH" />
        <Vehicle Make="INFINITY" Fuel="Gas" Price="16000" OnSale="N" Account="JOHNH" />
      </Region>
      <Region id="3"></Region>
      <Region id="4"></Region>
    </Vendor>
  </Geography>
</Response>

This is my Program.cs:

namespace XMLDeserializeExample
{
    class Program 
    {
        static void Main(string[] args)
        {
            string path = @"c:\XMLFile1.xml";
            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "Response";
            XmlSerializer ser = new XmlSerializer(typeof(ResponseGeographyVendorRegionVehicle), xRoot);
            ResponseGeographyVendorRegionVehicle i;
            using (Stream reader = new FileStream(path,FileMode.Open)) 
            {
                i = (ResponseGeographyVendorRegionVehicle)ser.Deserialize(reader);
                Console.WriteLine(i.Price);
                Console.ReadLine();
            }
        }

    }
}

This is the Paste Special Response.CS file that was created:

namespace XMLDeserializeExample
{
}

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Response
{

    private ResponseGeography geographyField;

    /// <remarks/>
    public ResponseGeography Geography
    {
        get
        {
            return this.geographyField;
        }
        set
        {
            this.geographyField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseGeography
{

    private ResponseGeographyVendor vendorField;

    /// <remarks/>
    public ResponseGeographyVendor Vendor
    {
        get
        {
            return this.vendorField;
        }
        set
        {
            this.vendorField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseGeographyVendor
{

    private ResponseGeographyVendorRegion[] regionField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Region")]
    public ResponseGeographyVendorRegion[] Region
    {
        get
        {
            return this.regionField;
        }
        set
        {
            this.regionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseGeographyVendorRegion
{

    private ResponseGeographyVendorRegionVehicle[] vehicleField;

    private byte idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Vehicle")]
    public ResponseGeographyVendorRegionVehicle[] Vehicle
    {
        get
        {
            return this.vehicleField;
        }
        set
        {
            this.vehicleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseGeographyVendorRegionVehicle
{

    private string makeField;

    private string fuelField;

    private ushort priceField;

    private string onSaleField;

    private string accountField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Make
    {
        get
        {
            return this.makeField;
        }
        set
        {
            this.makeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Fuel
    {
        get
        {
            return this.fuelField;
        }
        set
        {
            this.fuelField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ushort Price
    {
        get
        {
            return this.priceField;
        }
        set
        {
            this.priceField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string OnSale
    {
        get
        {
            return this.onSaleField;
        }
        set
        {
            this.onSaleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Account
    {
        get
        {
            return this.accountField;
        }
        set
        {
            this.accountField = value;
        }
    }
}

Please let me know how I can better explain myself. Apologies if none of this makes sense- welcome to my weekend lol.

Thank you.


Solution

  • As your xml-root is obviously a Response instead of a ResponseGeographyVendor you have to de-serialize to that type:

    string path = @"c:\XMLFile1.xml";
    XmlSerializer ser = new XmlSerializer(typeof(response);
    ResponseGeographyVendor i;
    using (Stream reader = new FileStream(path,FileMode.Open)) 
    {
        i = ((Response)ser.Deserialize(reader)).Geography.Vendor;
        Console.WriteLine(i.Price);
        Console.ReadLine();
    }
    

    A serializer will only work on the entire xml-document. You can´t just write or read parts of it. So just use the xml, serialize it to an instance of Response and get its Geography-member.

    Now you can easily get the 3rd Vehicle within the second Region:

    var vehicle = i.Region[1].Vehicle[2];
    

    Notice that you don´t need to provide the xml-root yourself.