0

My XML data structure looks like this..

  <SalesOrders>
    <SalesOrder>
      <Id>123</Id>
      ....
    </SalesOrder>
  </SalesOrders>

My C# code look like this.

 [XmlRoot(ElementName = "SalesOrders", Namespace = "")]
 public class SalesOrders : List<SalesOrder> { }
 public class SalesOrder {
    public int Id{get;set;}
 }

Deserializing this works fine, but I'm constrained to that the name of the class "SalesOrder" must match the name of the Tag < SalesOrder >. I'm not able to figure out howto decorate my list or my item in such a way that it is possible to have "name mismatch" on them.. Anyone..

PEtter
  • 806
  • 9
  • 22
  • You probably [don´t want to derive from `List`](https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt), but just have a class that has a list and an `Id`-property. – MakePeaceGreatAgain Apr 09 '19 at 08:00

1 Answers1

0

First off you usually won´t need to - and shouldn´t - inherit List<T>. Instead your class can have a list.

Furthermor you can use the Name-property of the Xml...Attribute in order to provide the different name in the xml than in your class-hierarch.

[XmlRoot(ElementName = "SalesOrders", Namespace = "")]
public class MyArbitraryName
{
    [XmlElement("SalesOrder")]
    public List<SalesOrder> Orders { get; set; }
}

public class SalesOrder 
{
    public int Id{get;set;}
}
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111