0
    [CollectionDataContract(Namespace = "http://schemas.datacontract.org/2004/07/ServicesTools.WebServices")]
    public class AcquisitionDetails : List<Acquisition>
    {


    }


    [DataContract(Name = "Acquisition", Namespace = "http://schemas.datacontract.org/2004/07/ServicesTools.WebServices")]
    public class Acquisition
    {
        [DataMember]
        public string GeoCode { get; set; }

        [DataMember]
        public string OrderDate { get; set; }

        [DataMember]
        public string Supplier { get; set; }

        [DataMember]
        public string PartNo { get; set; }

        [DataMember]
        public int QtyExpected { get; set; }

        [DataMember]
        public uint PONumber { get; set; }

    }

in a different question, we got this XML working:

<?xml version="1.0" encoding="utf-8"?>
<AcquisitionDetails>
  <Acquisition>
    <GeoCode>PHX</GeoCode>
    <OrderDate>3/10/2020 12:00:00 AM</OrderDate>
    <PartNo>20L6S52C11</PartNo>
    <QtyExpected>71</QtyExpected>
    <PONumber>1990001996</PONumber>
    <Supplier>Lenovo (International) BV</Supplier>
  </Acquisition>
</AcquisitionDetails>

Code in the Controller:

        [Consumes("application/xml")]
        [Produces("application/xml")]
        [HttpPost]
        public ActionResult Post(AcquisitionDetails ad)
        {
            return Ok(ad);
        }

BUT, there's a but;

when I sent this in POSTMAN, the PONumber for example, is empty

<AcquisitionDetails xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ServicesTools.WebServices">
    <Acquisition>
        <GeoCode>PHX</GeoCode>
        <OrderDate>3/10/2020 12:00:00 AM</OrderDate>
        <PONumber>0</PONumber>
        <PartNo>20L6S52C11</PartNo>
        <QtyExpected>71</QtyExpected>
        <Supplier>Lenovo (International) BV</Supplier>
    </Acquisition>
</AcquisitionDetails>

This is the case, until I move it up a bit, but then

    <Acquisition>
        <GeoCode i:nil="true" />
        <OrderDate i:nil="true" />
        <PONumber>1990001996</PONumber>
        <PartNo>20L6S52C11</PartNo>
        <QtyExpected>71</QtyExpected>
        <Supplier>Lenovo (International) BV</Supplier>
    </Acquisition>

anyway, long story short.. I cannot change the sequence of the incoming XML, nor do I want to be forced to receive the XML in said sequence; how can I instruct the model to parse the XML as it comes?

========update=====

    [DataContract(Name = "Acquisition", Namespace = "http://schemas.datacontract.org/2004/07/ServicesTools.WebServices")]
    public class Acquisition
    {
        [DataMember(Name = "GeoCode")]
        public string GeoCode { get; set; }

        [DataMember(Name = "OrderDate")]
        public string OrderDate { get; set; }

        [DataMember(Name = "Supplier")]
        public string Supplier { get; set; }

        [DataMember(Name = "PartNo")]
        public string PartNo { get; set; }

        [DataMember(Name = "QtyExpected")]
        public int QtyExpected { get; set; }

        [DataMember(Name = "PONumber")]
        public Int64 PONumber { get; set; }

    }

but, the results are still:

Input:

<?xml version="1.0" encoding="utf-8"?>
<AcquisitionDetails xmlns="http://schemas.datacontract.org/2004/07/ServicesTools.WebServices">
  <Acquisition>
    <GeoCode>PHX</GeoCode>
    <OrderDate>3/10/2020 12:00:00 AM</OrderDate>
    <PONumber>1990001996</PONumber>
    <PartNo>20L6S52C11</PartNo>
    <QtyExpected>71</QtyExpected>
    <Supplier>Lenovo (International) BV</Supplier>
  </Acquisition>
  <Acquisition>
    <GeoCode>PHX</GeoCode>
    <OrderDate>3/9/2020 12:00:00 AM</OrderDate>
    <PartNo>AA549668</PartNo>
    <QtyExpected>20</QtyExpected>
    <PONumber>1990001879</PONumber>
    <Supplier>SHI INTERNATIONAL CORP</Supplier>
  </Acquisition>
</AcquisitionDetails>

Output:

<AcquisitionDetails xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ServicesTools.WebServices">
    <Acquisition>
        <GeoCode>PHX</GeoCode>
        <OrderDate>3/10/2020 12:00:00 AM</OrderDate>
        <PONumber>1990001996</PONumber>
        <PartNo>20L6S52C11</PartNo>
        <QtyExpected>71</QtyExpected>
        <Supplier>Lenovo (International) BV</Supplier>
    </Acquisition>
    <Acquisition>
        <GeoCode>PHX</GeoCode>
        <OrderDate>3/9/2020 12:00:00 AM</OrderDate>
        <PONumber>0</PONumber>
        <PartNo>AA549668</PartNo>
        <QtyExpected>20</QtyExpected>
        <Supplier>SHI INTERNATIONAL CORP</Supplier>
    </Acquisition>
</AcquisitionDetails>
dbc
  • 104,963
  • 20
  • 228
  • 340
Fimlore
  • 147
  • 1
  • 9
  • There is no way to disable the order sensitivity of `DataContractSerializer`, see [WCF Disable Deserialization Order Sensitivity](https://stackoverflow.com/q/1727682/3744182) and [WCF Datacontract, some fields do not deserialize](https://stackoverflow.com/q/2519240/3744182). – dbc Sep 30 '20 at 15:53
  • If you are actually using [tag:asp.net-web-api] you can switch to `XmlSerializer` which is order-independent by default. See e.g. [ASP.NET Web API Controller Specific Serializer](https://stackoverflow.com/q/13383035/3744182) and https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization#xml-media-type-formatter – dbc Sep 30 '20 at 15:53
  • Alternative is to manually implement [`IXmlSerializable`](https://stackoverflow.com/q/279534/3744182) for `Acquisition` which is very burdensome to do correctly. – dbc Sep 30 '20 at 15:54
  • Thanks for your comments. I was using .net core, but that seems to give me more and more headaches, and since I will be hosting on Windows Server anyway, Ill see if the .net framework provides me more options; it at least has a easier implementation as it seems.. – Fimlore Oct 01 '20 at 09:37
  • You can use `XmlSerializer` with [tag:asp.net-core]. [`AddXmlSerializerFormatters()`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.mvcxmlmvcbuilderextensions.addxmlserializerformatters?view=aspnetcore-3.1) enables it. [`AddXmlDataContractSerializerFormatters()`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.mvcxmlmvcbuilderextensions.addxmldatacontractserializerformatters?view=aspnetcore-3.1) enables `DataContractSerializer`. – dbc Oct 01 '20 at 16:02
  • Note you need to use different attributes with `XmlSerializer`. See [Attributes That Control XML Serialization](https://learn.microsoft.com/en-us/dotnet/standard/serialization/attributes-that-control-xml-serialization). – dbc Oct 01 '20 at 16:04
  • @dbc Thanks for the link to the documentations! – Fimlore Oct 01 '20 at 17:03

2 Answers2

2

Are you try order for it? I am not sure but may be helpfull for you

[DataMember(Name = "GeoCode",Order =1)]
  • That makes sure the XML in this current sequence gets parsed correctly; but I cannot guarantee the XML to always be in the same sequence of elements.. – Fimlore Sep 25 '20 at 08:44
0

Specify Name in your DataMember attribute for all parameters - then order of parameters should not matter. For example:

    public class Acquisition
    {
        [DataMember(Name = "GeoCode"]
        public string GeoCode { get; set; }
    }
kosist
  • 2,868
  • 2
  • 17
  • 30
  • hm, I was hoping you were right, but (updatde in my question), as you can see: It does not help.. – Fimlore Sep 25 '20 at 08:30
  • Don't know if it matters to the question, but I am using: AddXmlDataContractSerializerFormatters – Fimlore Sep 25 '20 at 08:32