[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>
Are you try order for it? I am not sure but may be helpfull for you
[DataMember(Name = "GeoCode",Order =1)]
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; }
}
Related
I have created a REST Service which sends a response in XML. I have set the response format as XML and created the following Data Contracts:
[DataContract]
public class AuthorisationResult
{
[DataMember]
public string Status { get; set; }
[DataMember]
public Variable[] Variables { get; set; }
}
[DataContract]
public class Variable
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Type { get; set; }
}
This works ok however the output of the XML is not formatted as I need it. It is showing like this:
<Variables>
<Variable>
<Name>SomeName1</Name>
<Type>SomeType1</Type>
</Variable>
</Variables>
But I want to show it like this:
<Variables>
<Variable Name="SomeName1" Type="SomeType1"/>
</Variables>
Can anyone advise what I change to structure it how I want.
Just add XmlAttribute to the properties
[DataContract]
public class Variable
{
[DataMember, XmlAttribute]
public string Name { get; set; }
[DataMember, XmlAttribute]
public string Type { get; set; }
}
Here's my XML:
<Events>
<Event>
<content_id>6442452774</content_id>
<content_title>Title of the event</content_title>
<content_html>
<Date>2015-11-18</Date>
<EventType>Events</EventType>
<Description>
<p>this is an "event"</p>
</Description>
<Speakers>speaker1 LLC<br />speaker2<br />Jspeaker3</Speakers>
<Time>5:30 - 6:00pm Registration<br />6:00 - 7:00pm Panel Discussion<br />7:00 - 8:00pm Networking Reception</Time>
<Where></Where>
<RegistrationInfo>Please contact events#events.com to register for this event.</RegistrationInfo>
<Registration>false</Registration>
</content_html>
<date_created>2015-10-24T14:24:12.333</date_created>
<folder_id>262</folder_id>
<content_teaser>this is the content "can you display it."</content_teaser>
<content_text>text of the vent "more text" a lot of text here </content_text>
<end_date>2015-11-19T21:35:00</end_date>
<content_type>1</content_type>
<template_id>43</template_id>
<content_status>A</content_status>
</Event>
<Event>.... Other events </Event>
<Events>
and here's are my classes:
public class Serializable_Events
{
[XmlElement("Event")]
public List<Serializable_Event> EventList = new List<Serializable_Event>();
}
public class Serializable_Event
{
[XmlElement("content_id")]
public string content_id { get; set; }
[XmlElement("content_title")]
public string content_title { get; set; }
[XmlElement("content_html")]
public Serializable_Event_ContentHTML ContentHTML { get; set; }
[XmlText]
public string content_teaser { get; set; }
[XmlElement("content_text")]
public string content_text { get; set; }
}
public class Serializable_Event_ContentHTML
{
[XmlElement("Date")]
public string Date { get; set; }
[XmlElement("EventType")]
public string EventType { get; set; }
[XmlElement("Description")]
public string Description { get; set; }
[XmlElement("Speakers")]
public string Speakers { get; set; }
[XmlElement("Time")]
public string Time { get; set; }
[XmlElement("Where")]
public string Where { get; set; }
[XmlElement("RegistrationInfo")]
public string RegistrationInfo { get; set; }
[XmlElement("Registration")]
public bool Registration { get; set; }
//ignored html tags
[XmlIgnore]
public string p { get; set; }
[XmlIgnore]
public string br { get; set; }
[XmlIgnore]
public string a { get; set; }
}
Implementation:
XmlSerializer ser = new XmlSerializer(typeof(Serializable_Events));
var data = (Serializable_Events)ser.Deserialize(new StreamReader(#"events.xml"));
My problem is that some attributes are null and some are not (see the screenshot)
The ones that are null, like <Description> are due to malformed
XML.
You are storing HMTL directly in XML with text all over the place, and the serializer is not expecting that; further you are telling the serializer to ignore HTML tags using XmlIgnore which is intended for XML tags with properly formed XML. That's a wrong use of XmlIgnore
All XML which contains non-XML mark-up should be wrapped in CDATA sections; this will solve your problem. Further, you can remove all of the XmlIgnore code as well since it's not needed.
Your XML should look like this:
<Events>
<Event>
<content_id>6442452774</content_id>
<content_title>Title of the event</content_title>
<content_html>
<Date>2015-11-18</Date>
<EventType>Events</EventType>
<Description>
<![CDATA[<p>this is an ""event""</p>]]>
</Description>
<Speakers>
<![CDATA[speaker1 LLC<br />speaker2<br />Jspeaker3]]>
</Speakers>
<Time>
<![CDATA[5:30 - 6:00pm Registration<br />6:00 - 7:00pm Panel Discussion<br />7:00 - 8:00pm Networking Reception]]>
</Time>
<Where></Where>
<RegistrationInfo>
<![CDATA[Please contact <a href='mailto:events#events.com'>events#events.com</a> to register for this event.]]>
</RegistrationInfo>
<Registration>false</Registration>
</content_html>
<date_created>2015-10-24T14:24:12.333</date_created>
<folder_id>262</folder_id>
<content_teaser>this is the content 'can you display it.'</content_teaser>
<content_text>text of the vent 'more text' a lot of text here </content_text>
<end_date>2015-11-19T21:35:00</end_date>
<content_type>1</content_type>
<template_id>43</template_id>
<content_status>A</content_status>
</Event>
</Events>"
I've been looking on similar StackOverFlow posts but nothing seems to work.
I'm trying to deserialize an XML string that is being sent to me from a restful service using RestSharper. Basically, my problem is that some of the properties of my model class are being left null, and I have no clue why.
The model class I'm using is this:
class Photo
{
[SerializeAs(Name = "id")]
public long? id { get; set; }
[SerializeAs(Name = "title")]
public string title { get; set; }
[SerializeAs(Name = "description")]
public string description { get; set; }
[SerializeAs(Name = "size")]
public long? size { get; set; }
[SerializeAs(Name = "user")]
public User user { get; set; }
[SerializeAs(Name = "tags")]
public List<Tag> listTags { get; set; }
[SerializeAs(Name = "comments")]
public List<Comment> listComments { get; set; }
}
class Photos
{
[SerializeAs(Name = "photos")]
public List<Photo> listPhotos { get; set; }
}
The XML I'm trying to deserialize follows this structure :
<photos>
<photo>
<id>1</id>
<title>some title...</title>
<description>some description...</description>
<size>162798</size>
<user>
<id>4</id>
<name>John</name>
</user>
<tags>
<tag>
<id>1</id>
<value>some value...</value>
</tag>
</tags>
<comments>
<comment>
<id>1</id>
<value>some comment</value>
<author>admin</author>
</comment>
</comments>
</photo>
</photos>
Back to the C# code. The thing is that the list of comments, the list of tags, and the user object of the Photo class are being left null . Does anyone knows why is this happening?
I need to serialize an Object to XML and back. The XML is fix and I can't change it.
I fail to generate this structure after bookingList.
How can I "group" these <booking> elements to appear as a LIST and keep <error> & <counter> before this List of <booking> elements.
See my example here:
Structure i need....
<nicexml>
<key_id>1234567</key_id>
<surname>Jil</surname>
<name>Sander</name>
<station_id>1</station_id>
<ownBookings>
<bookingList>
<error></error>
<counter>20</counter>
<booking>
<bookingID>1234567890</bookingID>
</booking>
<booking>
<bookingID>2345678901</bookingID>
</booking>
</bookingList>
</ownBookings>
</nicexml>
Structure i get with C# code below....
<nicexml>
<key_id>1234567</key_id>
<surname>Jil</surname>
<name>Sander</name>
<station_id>1</station_id>
<ownBookings>
<bookingList>
<booking>
<booking>
<bookingID>1234567890</bookingID>
</booking>
<booking>
<bookingID>2345678901</bookingID>
</booking>
<booking>
<error></error>
<counter>20</counter>
</bookingList>
</ownBookings>
</nicexml>
C# Code:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace xml_objects_serials
{
public class bookings
{
public class nicexml
{
public string key_id
{ get; set; }
public string surname
{ get; set; }
public string name
{ get; set; }
public int station_id
{ get; set; }
public ownBookings ownBookings
{ get; set; }
}
public class ownBookings
{
public bookingList bookingList
{ get; set; }
}
public class bookingList {
public string error
{ get; set; }
public int counter
{ get; set; }
public List<booking> booking= new List<booking>();
}
public class booking
{
public int bookingID
{ get; set; }
}
}
Try decorating the properties of the bookingListclass with the XmlElementAttribute, in order to control how the objects of that class are going to be serialized to XML.
Here's an example:
public class bookingList
{
[XmlElement(Order = 1)]
public string error { get; set; }
[XmlElement(Order = 2)]
public int counter { get; set; }
[XmlElement(ElementName = "booking", Order = 3)]
public List<booking> bookings = new List<booking>();
}
public class booking
{
public int id { get; set; }
}
In my test I obtained this output:
<?xml version="1.0" ?>
<bookingList>
<error>sample</error>
<counter>0</counter>
<booking>
<id>1</id>
</booking>
<booking>
<id>2</id>
</booking>
<booking>
<id>3</id>
</booking>
</bookingList>
Related resources:
Controlling XML Serialization Using Attributes
I was facing with this problem and i solved it... Well it is very interesting, and this is a bug in .net maybe.
the problem is here:
public List<booking> booking= new List<booking>();
You should use:
public List<booking> booking { get; set; }
And you will get the defined order .... but why? who knows... :)
Good Afternoon,
I have the following classes
public class MaintenanceBundle
{
[XmlAttribute(AttributeName = "Required")]
public string Required { get; set; }
[XmlAttribute(AttributeName = "ID")]
public string Id { get; set; }
[XmlElement(ElementName = "Title")]
public string Title { get; set; }
[XmlElement(ElementName = "MntReason")]
public MaintenanceReason Reason { get; set; }
[XmlElement(ElementName = "Tasks")]
public MaintenanceBundleCollection Tasks { get; set; }
}
public class MaintenanceBundleCollection
{
[XmlElement(ElementName = "Task")]
public List<MaintenanceBundleTask> Tasks { get; set; }
}
public class MaintenanceReason
{
[XmlAttribute(AttributeName = "Every")]
public string Every { get; set; }
[XmlElement(ElementName = "Mileage", IsNullable = true)]
public int? Mileage { get; set; }
[XmlElement(ElementName = "Time", IsNullable = true)]
public TimeInterval TimeInterval { get; set; }
}
I'm trying to deserialize this xml to objects using these classes. Here is the XML
<MntBundle Required="Yes" ID="S08870641702009101200000">
<Title>DIRT OR DUSTY ROADS - 5000 MILES / 6 MONTHS</Title>
<MntReason Every="No">
<Mileage Unit="MILES">5000</Mileage>
</MntReason>
<Tasks>
<Task ID="4-2" />
<Task ID="4-3">
<NMVCQualifier>Drive Shaft Boots</NMVCQualifier>
<MVCQualifiers>
<Qualifier Name="Drive Type">4WD</Qualifier>
</MVCQualifiers>
</Task>
<Task ID="4-1" />
<Task ID="4-4" />
<Task ID="5-1">
<MVCQualifiers>
<Qualifier Name="Drive Type">4WD</Qualifier>
</MVCQualifiers>
</Task>
<Task ID="6-1" />
<Task ID="7-1" />
</Tasks>
</MntBundle>
For some reason I am unable to get the Mileage element inside the MntReason element. It keeps coming back as null. Any ideas what I am doing wrong? All the other elements seem to deserialize properly. I left out irrelevant classes from my post. If anyone has any pointers how I can properly retreive this value I would love to hear it. Thanks so much for any help.
Cheers,
~ck in San Diego
Untested but it should need something like the class below. I'm not sure how XmlText will behave with an integer.
public class Mileage
{
[XmlAttribute(AttributeName = "Unit")]
public string Unit {get; set;}
[XmlText]
public int Mileage {get; set;}
}