Manually populate object generated fom xsd in C# - c#

Sorry, new to all this...
I have an XSD file which I used to create classes in C# using XSD.exe.
The challenge now is trying to figure out how to manually populate the object so I can then serialise to XML (I won't have a source XML file so I need to manually set the properties via an alternate source).
Below is a sample classes generated from the XSD file. I'm able to set the filesummary and debtor information but no idea how to set the debtorcontact information.
<?xml version="1.0" encoding="utf-8"?>
<FileSummary FileDate="1900-01-01T01:01:01+08:00" OpeningBalance="1" Invoices="1" DebitNotes="1" CreditNotes="1" CashReceipts="1" OtherPositive="1" OtherNegative="1" ClosingBalance="1">
<Debtor DebtorId="DebtorId2" DebtorName="DebtorName2" ExcludedDebtor="ExcludedDebtor2" CreditLimit="-79228162514264337593543950335" DebtorStatus="DebtorStatus2" TradingTerms="TradingTerms2" OverduePeriod="4294967295" HeadOfficeCode="HeadOfficeCode2" HeadOfficeName="HeadOfficeName2" DebtorCountry="DebtorCountry2" ABN="ABN2" ARBN="ARBN2" ACN="ACN2" LegalName="LegalName2" MainTradingName="MainTradingName2" OtherTradingName="OtherTradingName2">
<DebtorContact Surname="Surname4" FirstName="FirstName4" OtherNames="OtherNames4" Phone="Phone4" Mobile="Mobile4" Fax="Fax4" Email="Email4" Salutation="Salutation4" JobTitle="JobTitle4" ContactAddress="ContactAddress4" ContactSuburb="ContactSuburb4" ContactCountry="ContactCountry4" ContactState="ContactState4" ContactPostcode="ContactPostcode4" />
<DebtorContact Surname="Surname5" FirstName="FirstName5" OtherNames="OtherNames5" Phone="Phone5" Mobile="Mobile5" Fax="Fax5" Email="Email5" Salutation="Salutation5" JobTitle="JobTitle5" ContactAddress="ContactAddress5" ContactSuburb="ContactSuburb5" ContactCountry="ContactCountry5" ContactState="ContactState5" ContactPostcode="ContactPostcode5" />
</Debtor>
</FileSummary>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.6.1055.0.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class FileSummary {
private FileSummaryDebtor[] debtorField;
private System.DateTime fileDateField;
private decimal openingBalanceField;
private decimal invoicesField;
private decimal debitNotesField;
private decimal creditNotesField;
private decimal cashReceiptsField;
private decimal otherPositiveField;
private decimal otherNegativeField;
private decimal closingBalanceField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Debtor")]
public FileSummaryDebtor[] Debtor {
get {
return this.debtorField;
}
set {
this.debtorField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime FileDate {
get {
return this.fileDateField;
}
set {
this.fileDateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal OpeningBalance {
get {
return this.openingBalanceField;
}
set {
this.openingBalanceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal Invoices {
get {
return this.invoicesField;
}
set {
this.invoicesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal DebitNotes {
get {
return this.debitNotesField;
}
set {
this.debitNotesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal CreditNotes {
get {
return this.creditNotesField;
}
set {
this.creditNotesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal CashReceipts {
get {
return this.cashReceiptsField;
}
set {
this.cashReceiptsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal OtherPositive {
get {
return this.otherPositiveField;
}
set {
this.otherPositiveField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal OtherNegative {
get {
return this.otherNegativeField;
}
set {
this.otherNegativeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal ClosingBalance {
get {
return this.closingBalanceField;
}
set {
this.closingBalanceField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class FileSummaryDebtor {
private object[] itemsField;
private string debtorIdField;
private string debtorNameField;
private string excludedDebtorField;
private decimal creditLimitField;
private string debtorStatusField;
private string tradingTermsField;
private uint overduePeriodField;
private string headOfficeCodeField;
private string headOfficeNameField;
private string debtorCountryField;
private string aBNField;
private string aRBNField;
private string aCNField;
private string legalNameField;
private string mainTradingNameField;
private string otherTradingNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DebtorContact", typeof(FileSummaryDebtorDebtorContact))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DebtorId {
get {
return this.debtorIdField;
}
set {
this.debtorIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DebtorName {
get {
return this.debtorNameField;
}
set {
this.debtorNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ExcludedDebtor {
get {
return this.excludedDebtorField;
}
set {
this.excludedDebtorField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal CreditLimit {
get {
return this.creditLimitField;
}
set {
this.creditLimitField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DebtorStatus {
get {
return this.debtorStatusField;
}
set {
this.debtorStatusField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string TradingTerms {
get {
return this.tradingTermsField;
}
set {
this.tradingTermsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public uint OverduePeriod {
get {
return this.overduePeriodField;
}
set {
this.overduePeriodField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string HeadOfficeCode {
get {
return this.headOfficeCodeField;
}
set {
this.headOfficeCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string HeadOfficeName {
get {
return this.headOfficeNameField;
}
set {
this.headOfficeNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DebtorCountry {
get {
return this.debtorCountryField;
}
set {
this.debtorCountryField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ABN {
get {
return this.aBNField;
}
set {
this.aBNField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ARBN {
get {
return this.aRBNField;
}
set {
this.aRBNField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ACN {
get {
return this.aCNField;
}
set {
this.aCNField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string LegalName {
get {
return this.legalNameField;
}
set {
this.legalNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string MainTradingName {
get {
return this.mainTradingNameField;
}
set {
this.mainTradingNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string OtherTradingName {
get {
return this.otherTradingNameField;
}
set {
this.otherTradingNameField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class FileSummaryDebtorDebtorContact {
private string surnameField;
private string firstNameField;
private string otherNamesField;
private string phoneField;
private string mobileField;
private string faxField;
private string emailField;
private string salutationField;
private string jobTitleField;
private string contactAddressField;
private string contactSuburbField;
private string contactCountryField;
private string contactStateField;
private string contactPostcodeField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Surname {
get {
return this.surnameField;
}
set {
this.surnameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string FirstName {
get {
return this.firstNameField;
}
set {
this.firstNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string OtherNames {
get {
return this.otherNamesField;
}
set {
this.otherNamesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Phone {
get {
return this.phoneField;
}
set {
this.phoneField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Mobile {
get {
return this.mobileField;
}
set {
this.mobileField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Fax {
get {
return this.faxField;
}
set {
this.faxField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Email {
get {
return this.emailField;
}
set {
this.emailField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Salutation {
get {
return this.salutationField;
}
set {
this.salutationField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string JobTitle {
get {
return this.jobTitleField;
}
set {
this.jobTitleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactAddress {
get {
return this.contactAddressField;
}
set {
this.contactAddressField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactSuburb {
get {
return this.contactSuburbField;
}
set {
this.contactSuburbField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactCountry {
get {
return this.contactCountryField;
}
set {
this.contactCountryField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactState {
get {
return this.contactStateField;
}
set {
this.contactStateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ContactPostcode {
get {
return this.contactPostcodeField;
}
set {
this.contactPostcodeField = value;
}
}
}
using System;
namespace CoinsAssetWatchXMLGen
{
class Program
{
static void Main(string[] args)
{
FileSummary fileSummary = new FileSummary();
fileSummary.FileDate = DateTime.Today;
FileSummaryDebtor debtor = new FileSummaryDebtor;
debtor.DebtorId = "11";
debtor.DebtorName = "fred";
debtor.DebtorStatus = "active";
debtor.ABN = "ABC1234567";
fileSummary.Debtor[1] = debtor;
//how do I associate this following with a debtor for XML??
FileSummaryDebtorDebtorContact contact = new FileSummaryDebtorDebtorContact();
}
}
}

The approach I took was to create a dataset classes using xsd.exe /d.
Once the data set classes were created
I initially populated the data table as follows just to get a better understanding of the data set structure:
NewDataSet newDataSet = new NewDataSet();
newDataSet.ReadXml("FileSummary.xml");

Related

Constructing GET Request using WSDL Service Reference

I'm connecting to the clothing distributor SanMar using their wsdl endpoint. I've created a service reference for it but where I'm hitting a roadblock is in a specific method I want to call. It requires two params, the one that's giving me issues is an item[]. The wsdl documentation has it's own definition of an item[] which I can't seem to satisfy.
The endpoint is https://ws.sanmar.com:8080/SanMarWebServices/SanMarNotificationServicePort?WSDL
The documentation for the item[] object is:
public partial class item : object, System.ComponentModel.INotifyPropertyChanged {
private double casePriceField;
private bool casePriceFieldSpecified;
private string colorField;
private double dozenPriceField;
private bool dozenPriceFieldSpecified;
private string inventoryKeyField;
private double myPriceField;
private bool myPriceFieldSpecified;
private double piecePriceField;
private bool piecePriceFieldSpecified;
private double salePriceField;
private bool salePriceFieldSpecified;
private string sizeField;
private int sizeIndexField;
private bool sizeIndexFieldSpecified;
private string styleField;
private string saleStartDateField;
private string saleEndDateField;
private double incentivePriceField;
private bool incentivePriceFieldSpecified;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
public double casePrice {
get {
return this.casePriceField;
}
set {
this.casePriceField = value;
this.RaisePropertyChanged("casePrice");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool casePriceSpecified {
get {
return this.casePriceFieldSpecified;
}
set {
this.casePriceFieldSpecified = value;
this.RaisePropertyChanged("casePriceSpecified");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=1)]
public string color {
get {
return this.colorField;
}
set {
this.colorField = value;
this.RaisePropertyChanged("color");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=2)]
public double dozenPrice {
get {
return this.dozenPriceField;
}
set {
this.dozenPriceField = value;
this.RaisePropertyChanged("dozenPrice");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool dozenPriceSpecified {
get {
return this.dozenPriceFieldSpecified;
}
set {
this.dozenPriceFieldSpecified = value;
this.RaisePropertyChanged("dozenPriceSpecified");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=3)]
public string inventoryKey {
get {
return this.inventoryKeyField;
}
set {
this.inventoryKeyField = value;
this.RaisePropertyChanged("inventoryKey");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=4)]
public double myPrice {
get {
return this.myPriceField;
}
set {
this.myPriceField = value;
this.RaisePropertyChanged("myPrice");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool myPriceSpecified {
get {
return this.myPriceFieldSpecified;
}
set {
this.myPriceFieldSpecified = value;
this.RaisePropertyChanged("myPriceSpecified");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=5)]
public double piecePrice {
get {
return this.piecePriceField;
}
set {
this.piecePriceField = value;
this.RaisePropertyChanged("piecePrice");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool piecePriceSpecified {
get {
return this.piecePriceFieldSpecified;
}
set {
this.piecePriceFieldSpecified = value;
this.RaisePropertyChanged("piecePriceSpecified");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=6)]
public double salePrice {
get {
return this.salePriceField;
}
set {
this.salePriceField = value;
this.RaisePropertyChanged("salePrice");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool salePriceSpecified {
get {
return this.salePriceFieldSpecified;
}
set {
this.salePriceFieldSpecified = value;
this.RaisePropertyChanged("salePriceSpecified");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=7)]
public string size {
get {
return this.sizeField;
}
set {
this.sizeField = value;
this.RaisePropertyChanged("size");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=8)]
public int sizeIndex {
get {
return this.sizeIndexField;
}
set {
this.sizeIndexField = value;
this.RaisePropertyChanged("sizeIndex");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool sizeIndexSpecified {
get {
return this.sizeIndexFieldSpecified;
}
set {
this.sizeIndexFieldSpecified = value;
this.RaisePropertyChanged("sizeIndexSpecified");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=9)]
public string style {
get {
return this.styleField;
}
set {
this.styleField = value;
this.RaisePropertyChanged("style");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=10)]
public string saleStartDate {
get {
return this.saleStartDateField;
}
set {
this.saleStartDateField = value;
this.RaisePropertyChanged("saleStartDate");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=11)]
public string saleEndDate {
get {
return this.saleEndDateField;
}
set {
this.saleEndDateField = value;
this.RaisePropertyChanged("saleEndDate");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=12)]
public double incentivePrice {
get {
return this.incentivePriceField;
}
set {
this.incentivePriceField = value;
this.RaisePropertyChanged("incentivePrice");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool incentivePriceSpecified {
get {
return this.incentivePriceFieldSpecified;
}
set {
this.incentivePriceFieldSpecified = value;
this.RaisePropertyChanged("incentivePriceSpecified");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
Can anyone point me in the right direction or pm me to talk further?
Thank you
I tried creating an item[] from scratch but I believe I'm supposed to be using the methods to set the values.

Serialization failure of nested data

The auto-generated WCF classes created by visual studio are not deserializing a SOAP response correctly for an endpoint I am using.
It is returning null for an object that is definitely being returned to the the service.
here is the class generated from wsdl
public partial class OrderDepartureLoadRail : object, System.ComponentModel.INotifyPropertyChanged {
private string request_NumberField;
private string agreement_NumberField;
private System.DateTime agreement_DataField;
private System.DateTime eTD_StartField;
private System.DateTime eTD_EndField;
private OrderDepartureLoadRailCONTAINERS cONTAINERSField;
private cargo[] cARGOSField;
private contractor[] cONTRACTORSField;
private string station_Code_ToField;
private string port_Code_ToField;
private string commentField;
private contact contactPersonField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://objects.common.exchange.terminal.service.cyberlines", Order=0)]
public string Request_Number {
get {
return this.request_NumberField;
}
set {
this.request_NumberField = value;
this.RaisePropertyChanged("Request_Number");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://objects.common.exchange.terminal.service.cyberlines", Order=1)]
public string Agreement_Number {
get {
return this.agreement_NumberField;
}
set {
this.agreement_NumberField = value;
this.RaisePropertyChanged("Agreement_Number");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://objects.common.exchange.terminal.service.cyberlines", Order=2)]
public System.DateTime Agreement_Data {
get {
return this.agreement_DataField;
}
set {
this.agreement_DataField = value;
this.RaisePropertyChanged("Agreement_Data");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://objects.common.exchange.terminal.service.cyberlines", Order=3)]
public System.DateTime ETD_Start {
get {
return this.eTD_StartField;
}
set {
this.eTD_StartField = value;
this.RaisePropertyChanged("ETD_Start");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace="http://objects.common.exchange.terminal.service.cyberlines", Order=4)]
public System.DateTime ETD_End {
get {
return this.eTD_EndField;
}
set {
this.eTD_EndField = value;
this.RaisePropertyChanged("ETD_End");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=5)]
public OrderDepartureLoadRailCONTAINERS CONTAINERS {
get {
return this.cONTAINERSField;
}
set {
this.cONTAINERSField = value;
this.RaisePropertyChanged("CONTAINERS");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=6)]
[System.Xml.Serialization.XmlArrayItemAttribute("Cargo", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public cargo[] CARGOS {
get {
return this.cARGOSField;
}
set {
this.cARGOSField = value;
this.RaisePropertyChanged("CARGOS");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=7)]
[System.Xml.Serialization.XmlArrayItemAttribute("Contractor", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public contractor[] CONTRACTORS {
get {
return this.cONTRACTORSField;
}
set {
this.cONTRACTORSField = value;
this.RaisePropertyChanged("CONTRACTORS");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="token", Order=8)]
public string Station_Code_To {
get {
return this.station_Code_ToField;
}
set {
this.station_Code_ToField = value;
this.RaisePropertyChanged("Station_Code_To");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="token", Order=9)]
public string Port_Code_To {
get {
return this.port_Code_ToField;
}
set {
this.port_Code_ToField = value;
this.RaisePropertyChanged("Port_Code_To");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=10)]
public string Comment {
get {
return this.commentField;
}
set {
this.commentField = value;
this.RaisePropertyChanged("Comment");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=11)]
public contact ContactPerson {
get {
return this.contactPersonField;
}
set {
this.contactPersonField = value;
this.RaisePropertyChanged("ContactPerson");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
when transferring to web api xml document in format
<OrderDepartureLoadRail>
<Request_Number>1128606</Request_Number>
<Agreement_Number>ВЖЭ-13/492</Agreement_Number>
<Agreement_Data>2013-11-19T00:00:00</Agreement_Data>
<ETD_Start>2020-09-28T00:00:00</ETD_Start>
<ETD_End>2020-09-28T23:59:59</ETD_End>
<CONTAINERS>
<Container>
<ContNumber>TCNU802</ContNumber>
<ContQuantity>1</ContQuantity>
<ContType>40HC</ContType>
<ContOwner>COC</ContOwner>
<Weight>180</Weight>
<Tare>41</Tare>
</Container>
<SEALS>
<seal>
<Seals_Number>У965</Seals_Number>
</seal>
</SEALS>
</CONTAINERS>
<CARGOS>
<cargo>
<ETSNG>263</ETSNG>
<NumberOfSeats>20</NumberOfSeats>
<Pack>BG</Pack>
<Weight>180</Weight>
<Guard>false</Guard>
<Danger>false</Danger>
<OrderNo>1</OrderNo>
</cargo>
</CARGOS>
<CONTRACTORS>
<contractor>
<Contractor_Type>SH</Contractor_Type>
<Name>ПАО </Name>
<OKPO>01126016</OKPO>
<SendersMarks>Получ </SendersMarks>
<Comment>После</Comment>
</contractor>
</CONTRACTORS>
<Station_Code_To>9807</Station_Code_To>
<Port_Code_To>Кор</Port_Code_To>
<Comment />
<ContactPerson>
<FIO>Хузе</FIO>
<Phone>+79454</Phone>
<Mail>KKh#ya.ru</Mail>
</ContactPerson>
</OrderDepartureLoadRail>
ignores order and returns null(data = null)
[HttpPost]
[Route("PostCreateOrderDepartureLoadRail")]
public OrderResponse PostCreateOrderDepartureLoadRail(OrderDepartureLoadRail data)
{
...
}
here they describe a similar error, but they did not find a solution
github-solution
I managed to solve this problem using a third-party library YAXLib
https://github.com/sinairv/YAXLib

Creating a model class for XML

How to create a model class for the below xml.
<?xml version="1.0"?>
<NOTIFICATION>
<NOTIF_HEADER>
<SOURCE>FCDB</SOURCE>
<MSGID>585995798502256</MSGID>
<TXN_ID>UTP</TXN_ID>
</NOTIF_HEADER>
<NOTIF_BODY>
<UserDetail>
<USER_NAME>Mr Customer</USER_NAME>
<USER_EMAIL>MrCustomer#email.com</USER_EMAIL>
<USER_MO>0500000007</USER_MO>
</UserDetail>
<CAS_Token>
<PASS>
<![CDATA[[PASS]]]>
</PASS>
<GEMALTO>N</GEMALTO>
</CAS_Token>
</NOTIF_BODY>
</NOTIFICATION>
I want to serialize this xml. For that I tried to create model class, but didn't worked.
Here is your class:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class NOTIFICATION
{
private NOTIFICATIONNOTIF_HEADER nOTIF_HEADERField;
private NOTIFICATIONNOTIF_BODY nOTIF_BODYField;
/// <remarks/>
public NOTIFICATIONNOTIF_HEADER NOTIF_HEADER
{
get
{
return this.nOTIF_HEADERField;
}
set
{
this.nOTIF_HEADERField = value;
}
}
/// <remarks/>
public NOTIFICATIONNOTIF_BODY NOTIF_BODY
{
get
{
return this.nOTIF_BODYField;
}
set
{
this.nOTIF_BODYField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NOTIFICATIONNOTIF_HEADER
{
private string sOURCEField;
private ulong mSGIDField;
private string tXN_IDField;
/// <remarks/>
public string SOURCE
{
get
{
return this.sOURCEField;
}
set
{
this.sOURCEField = value;
}
}
/// <remarks/>
public ulong MSGID
{
get
{
return this.mSGIDField;
}
set
{
this.mSGIDField = value;
}
}
/// <remarks/>
public string TXN_ID
{
get
{
return this.tXN_IDField;
}
set
{
this.tXN_IDField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NOTIFICATIONNOTIF_BODY
{
private NOTIFICATIONNOTIF_BODYUserDetail userDetailField;
private NOTIFICATIONNOTIF_BODYCAS_Token cAS_TokenField;
/// <remarks/>
public NOTIFICATIONNOTIF_BODYUserDetail UserDetail
{
get
{
return this.userDetailField;
}
set
{
this.userDetailField = value;
}
}
/// <remarks/>
public NOTIFICATIONNOTIF_BODYCAS_Token CAS_Token
{
get
{
return this.cAS_TokenField;
}
set
{
this.cAS_TokenField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NOTIFICATIONNOTIF_BODYUserDetail
{
private string uSER_NAMEField;
private string uSER_EMAILField;
private uint uSER_MOField;
/// <remarks/>
public string USER_NAME
{
get
{
return this.uSER_NAMEField;
}
set
{
this.uSER_NAMEField = value;
}
}
/// <remarks/>
public string USER_EMAIL
{
get
{
return this.uSER_EMAILField;
}
set
{
this.uSER_EMAILField = value;
}
}
/// <remarks/>
public uint USER_MO
{
get
{
return this.uSER_MOField;
}
set
{
this.uSER_MOField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class NOTIFICATIONNOTIF_BODYCAS_Token
{
private string pASSField;
private string gEMALTOField;
/// <remarks/>
public string PASS
{
get
{
return this.pASSField;
}
set
{
this.pASSField = value;
}
}
/// <remarks/>
public string GEMALTO
{
get
{
return this.gEMALTOField;
}
set
{
this.gEMALTOField = value;
}
}
}
You can generate in yourself with VS:
Edit -> Paste Special -> Paste XML as classes

Adding information to an array within a class

I have converted some xml into classes using xsd and now I am having an issue getting some data into the array that exists in the class to add a list of items. I am trying to get the info into OrderItemsItem.
A bit stumped, any help appreciated (not really keen on making any changes to the classes if I can get away with it, convert to List<> etc) .
This is the code to add the info:
OrderItemsItem orderItemsItem = new OrderItemsItem();
orderItemsItem.CostCentre = "sfsdf";
orderItemsItem.DeliveryDate = "2014-01-05";
orderItemsItem.Fund = "G";
orderItemsItem.ExternalLineRef = "1";
orderItemsItem.ItemName = "dfss";
orderItemsItem.LineNo = "1";
orderItemsItem.ProdId = "dfsf";
orderItemsItem.Project = "";
orderItemsItem.QuantityOrdered = "2";
orderItemsItem.UnitCost = "10";
Order order = new Order();
OrderItemsItem [] items = {orderItemsItem};
order.Items.Item = items;
The Error I am receiving is happening on -> order.Items.Item = items;:
System.NullReferenceException: Object reference not set to an instance
of an object.
This is the Class:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18444
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XXXXXXXXX", IsNullable=false)]
public partial class Order {
private string referenceField;
private string notesField;
private string orderDateField;
private string statusField;
private OrderItems itemsField;
private OrderBuyerDetails buyerDetailsField;
/// <remarks/>
public string Reference {
get {
return this.referenceField;
}
set {
this.referenceField = value;
}
}
/// <remarks/>
public string Notes {
get {
return this.notesField;
}
set {
this.notesField = value;
}
}
/// <remarks/>
public string OrderDate {
get {
return this.orderDateField;
}
set {
this.orderDateField = value;
}
}
/// <remarks/>
public string Status {
get {
return this.statusField;
}
set {
this.statusField = value;
}
}
/// <remarks/>
public OrderItems Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
public OrderBuyerDetails BuyerDetails {
get {
return this.buyerDetailsField;
}
set {
this.buyerDetailsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderItems {
private OrderItemsItem[] itemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Item")]
public OrderItemsItem[] Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderItemsItem {
private string lineNoField;
private string externalLineRefField;
private string prodIdField;
private string itemNameField;
private string quantityOrderedField;
private string unitCostField;
private string deliveryDateField;
private string costCentreField;
private string projectField;
private string fundField;
/// <remarks/>
public string LineNo {
get {
return this.lineNoField;
}
set {
this.lineNoField = value;
}
}
/// <remarks/>
public string ExternalLineRef {
get {
return this.externalLineRefField;
}
set {
this.externalLineRefField = value;
}
}
/// <remarks/>
public string ProdId {
get {
return this.prodIdField;
}
set {
this.prodIdField = value;
}
}
/// <remarks/>
public string ItemName {
get {
return this.itemNameField;
}
set {
this.itemNameField = value;
}
}
/// <remarks/>
public string QuantityOrdered {
get {
return this.quantityOrderedField;
}
set {
this.quantityOrderedField = value;
}
}
/// <remarks/>
public string UnitCost {
get {
return this.unitCostField;
}
set {
this.unitCostField = value;
}
}
/// <remarks/>
public string DeliveryDate {
get {
return this.deliveryDateField;
}
set {
this.deliveryDateField = value;
}
}
/// <remarks/>
public string CostCentre {
get {
return this.costCentreField;
}
set {
this.costCentreField = value;
}
}
/// <remarks/>
public string Project {
get {
return this.projectField;
}
set {
this.projectField = value;
}
}
/// <remarks/>
public string Fund {
get {
return this.fundField;
}
set {
this.fundField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderBuyerDetails {
private string nameField;
private string emailField;
private OrderBuyerDetailsBillingAddress billingAddressField;
/// <remarks/>
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
public string Email {
get {
return this.emailField;
}
set {
this.emailField = value;
}
}
/// <remarks/>
public OrderBuyerDetailsBillingAddress BillingAddress {
get {
return this.billingAddressField;
}
set {
this.billingAddressField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderBuyerDetailsBillingAddress {
private string nameField;
private string address1Field;
private string address2Field;
private string address3Field;
private string placeField;
private string countyField;
private string postCodeField;
private string countryField;
private string emailField;
/// <remarks/>
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
public string Address1 {
get {
return this.address1Field;
}
set {
this.address1Field = value;
}
}
/// <remarks/>
public string Address2 {
get {
return this.address2Field;
}
set {
this.address2Field = value;
}
}
/// <remarks/>
public string Address3 {
get {
return this.address3Field;
}
set {
this.address3Field = value;
}
}
/// <remarks/>
public string Place {
get {
return this.placeField;
}
set {
this.placeField = value;
}
}
/// <remarks/>
public string County {
get {
return this.countyField;
}
set {
this.countyField = value;
}
}
/// <remarks/>
public string PostCode {
get {
return this.postCodeField;
}
set {
this.postCodeField = value;
}
}
/// <remarks/>
public string Country {
get {
return this.countryField;
}
set {
this.countryField = value;
}
}
/// <remarks/>
public string Email {
get {
return this.emailField;
}
set {
this.emailField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XXXXXXXXX", IsNullable=false)]
public partial class Post_Printondemand_Create_Full_Order_DataSet {
private Order[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Order")]
public Order[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
Thank you
order.Items.Item = items
The error must be that some order among your orders have its Items collection null (and thus making it not possible to access its "Item" property).
My suspicion is that in your example order.Items is null - you will need to attach a debugger and examine the variables yourself to determine the root cause of your null ref. Ensure that the array is not null before working with it.
in your example you're overriding an element of an array. This will either replace the existing element (if it exists) or (I believe) throw an IndexOutOfBoundsException if it doesn't.
Arrays are generally for fixed lengths of data, ones which you don't append/remove data from frequently. Lists are a better collection to use as they have Add() and Remove() functions for specifically this reason.
It is possible to "append" to C# arrays
order.Items = Order.Items.Union(new Item[]{myNewItem}));
However it's not very nice and creates a new array by merging the old ones! My advice... make the collection a list!

Programmatically controlling the order of xml elements

I'm using a third part web service[WS] in my .Net app.
My application is using the entity classes generated from this WS's wsdl.
I fetch the data from db, fill it in entity objects and then generate an xml out of it using XmlSerializer class.
One of the methods of the WS requires this xml string as input.And the xml should have elements in the same order as expected by the WS.But whats happening is that some of the elements are getting upside down in my app and so WS is throwing an innermost exception saying on serialzation:.
_innerException {"Inconsistent sequencing: if used on one of the class's members, the 'Order' property is required on all particle-like members, please explicitly set 'Order' using XmlElement, XmlAnyElement or XmlArray custom attribute on class member 'instrument'."} System.Exception {System.InvalidOperationException}
XmlSerializer serializer = new XmlSerializer(typeof(totemmessage));
So,my question here is, how do I programmatically control the order of these xml elements in my app before sending it to the WS?Note:I dont want to use xslt for this purpose.
Thanks for reading.
Here are my entity classes:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName="totem-message")]
[System.Xml.Serialization.XmlRootAttribute("totem", Namespace="", IsNullable=false)]
public partial class totemmessage {
private object itemField;
private ItemChoiceType1 itemElementNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("error", typeof(errorinfo))]
[System.Xml.Serialization.XmlElementAttribute("parseReport", typeof(parseReportinfo))]
[System.Xml.Serialization.XmlElementAttribute("results", typeof(templateinfo))]
[System.Xml.Serialization.XmlElementAttribute("subareas", typeof(subareasinfo))]
[System.Xml.Serialization.XmlElementAttribute("template", typeof(templateinfo))]
[System.Xml.Serialization.XmlElementAttribute("upload", typeof(templateinfo))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
[System.Xml.Serialization.XmlElement(Order = 0)]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 1)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType1 ItemElementName {
get {
return this.itemElementNameField;
}
set {
this.itemElementNameField = value;
}
}
}
[System.Xml.Serialization.XmlIncludeAttribute(typeof(energyInstrument))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public abstract partial class abstractEnergyInstrument {
private energyContractTime periodField;
private bool periodFieldSpecified;
private System.DateTime startDateField;
private bool startDateFieldSpecified;
private System.DateTime endDateField;
private bool endDateFieldSpecified;
private System.DateTime expiryDateField;
private bool expiryDateFieldSpecified;
private energyInstrumentClassifier typeField;
private bool typeFieldSpecified;
private string strikeField;
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 0)]
public energyContractTime period {
get {
return this.periodField;
}
set {
this.periodField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 1)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool periodSpecified {
get {
return this.periodFieldSpecified;
}
set {
this.periodFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="date", Order =2)]
public System.DateTime startDate {
get {
return this.startDateField;
}
set {
this.startDateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 3)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool startDateSpecified {
get {
return this.startDateFieldSpecified;
}
set {
this.startDateFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="date", Order =4)]
public System.DateTime endDate {
get {
return this.endDateField;
}
set {
this.endDateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 5)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool endDateSpecified {
get {
return this.endDateFieldSpecified;
}
set {
this.endDateFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="date", Order =6)]
public System.DateTime expiryDate {
get {
return this.expiryDateField;
}
set {
this.expiryDateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 7)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool expiryDateSpecified {
get {
return this.expiryDateFieldSpecified;
}
set {
this.expiryDateFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 8)]
public energyInstrumentClassifier type {
get {
return this.typeField;
}
set {
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 9)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool typeSpecified {
get {
return this.typeFieldSpecified;
}
set {
this.typeFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 10)]
public string strike {
get {
return this.strikeField;
}
set {
this.strikeField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("instrument", Namespace="", IsNullable=false)]
public partial class energyInstrument : abstractEnergyInstrument {
private decimal priceField;
private bool priceFieldSpecified;
private decimal forwardField;
private bool forwardFieldSpecified;
private decimal volField;
private bool volFieldSpecified;
private decimal consensusPriceField;
private bool consensusPriceFieldSpecified;
private decimal compositePriceField;
private bool compositePriceFieldSpecified;
private decimal reconstitutedForwardField;
private bool reconstitutedForwardFieldSpecified;
private decimal consensusVolField;
private bool consensusVolFieldSpecified;
private decimal compositeVolField;
private bool compositeVolFieldSpecified;
private string priceOutField;
private decimal priceRangeField;
private bool priceRangeFieldSpecified;
private decimal priceStddevField;
private bool priceStddevFieldSpecified;
private string volOutField;
private decimal volRangeField;
private bool volRangeFieldSpecified;
private decimal volStddevField;
private bool volStddevFieldSpecified;
private string contributorsField;
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 0)]
public decimal price {
get {
return this.priceField;
}
set {
this.priceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 1)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool priceSpecified {
get {
return this.priceFieldSpecified;
}
set {
this.priceFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 2)]
public decimal forward {
get {
return this.forwardField;
}
set {
this.forwardField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 3)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool forwardSpecified {
get {
return this.forwardFieldSpecified;
}
set {
this.forwardFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 4)]
public decimal vol {
get {
return this.volField;
}
set {
this.volField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 5)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool volSpecified {
get {
return this.volFieldSpecified;
}
set {
this.volFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 6)]
public decimal consensusPrice {
get {
return this.consensusPriceField;
}
set {
this.consensusPriceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 7)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool consensusPriceSpecified {
get {
return this.consensusPriceFieldSpecified;
}
set {
this.consensusPriceFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 8)]
public decimal compositePrice {
get {
return this.compositePriceField;
}
set {
this.compositePriceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 9)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool compositePriceSpecified {
get {
return this.compositePriceFieldSpecified;
}
set {
this.compositePriceFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 10)]
public decimal reconstitutedForward {
get {
return this.reconstitutedForwardField;
}
set {
this.reconstitutedForwardField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 11)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool reconstitutedForwardSpecified {
get {
return this.reconstitutedForwardFieldSpecified;
}
set {
this.reconstitutedForwardFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 12)]
public decimal consensusVol {
get {
return this.consensusVolField;
}
set {
this.consensusVolField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 13)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool consensusVolSpecified {
get {
return this.consensusVolFieldSpecified;
}
set {
this.consensusVolFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 14)]
public decimal compositeVol {
get {
return this.compositeVolField;
}
set {
this.compositeVolField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 15)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool compositeVolSpecified {
get {
return this.compositeVolFieldSpecified;
}
set {
this.compositeVolFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 16)]
public string priceOut {
get {
return this.priceOutField;
}
set {
this.priceOutField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 17)]
public decimal priceRange {
get {
return this.priceRangeField;
}
set {
this.priceRangeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 18)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool priceRangeSpecified {
get {
return this.priceRangeFieldSpecified;
}
set {
this.priceRangeFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 19)]
public decimal priceStddev
{
get {
return this.priceStddevField;
}
set {
this.priceStddevField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 20)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool priceStddevSpecified {
get {
return this.priceStddevFieldSpecified;
}
set {
this.priceStddevFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 21)]
public string volOut {
get {
return this.volOutField;
}
set {
this.volOutField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 22)]
public decimal volRange {
get {
return this.volRangeField;
}
set {
this.volRangeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 23)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool volRangeSpecified {
get {
return this.volRangeFieldSpecified;
}
set {
this.volRangeFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 24)]
public decimal volStddev {
get {
return this.volStddevField;
}
set {
this.volStddevField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElement(Order = 25)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool volStddevSpecified {
get {
return this.volStddevFieldSpecified;
}
set {
this.volStddevFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order =26)]
public string contributors {
get {
return this.contributorsField;
}
set {
this.contributorsField = value;
}
}
}
I used XmlElement[Order =n] on top of each of the properties in the entity classes. So, after playing around with the ordering, I could made this serialization work.Inheritance and partial classes made this fix all the more difficult!Thanks.
The XmlSerializer generates the elements in the order of declaration in the class - can you just change the order in the entity?
How to use Order = N together with other XML attributes and List collection.
public class MyClass
{
[XmlAnyElement("My-XLMComment", Order = 3)] public XmlComment CommentA { get; set; } = new XmlDocument().CreateComment("This is an XML comment");
[XmlAttribute("MyAttribute")] public string str4 { get; set; } //This is a XML-Attribute, Order not applicable
[XmlIgnore] public string str1 { get; set; } //This is a [XmlIgnore], Order not applicable
[XmlElement(Order = 5)] public string str2 { get; set; } // Standard property with Order
[XmlElement(Order = 2)] public string str3 { get; set; } // Standard property with Order
[XmlElement(Order = 1)] public List<MyList> ListElms = new List<MyList> //List collection with Order
{
new ListElm {str1 = "xxx", str2 ="yyy", str3 ="zzz"} ,
new ListElm {str1 = "xxx", str2 ="yyy", str3 ="zzz"}
};
public class MyList
{
[XmlElement(Order = 3)] public string str1 { get; set; } // Order can be assigned, but not mandatory
[XmlElement(Order = 2)] public string str2 { get; set; } // Order can be assigned, but not mandatory
[XmlElement(Order = 1)] public string str3 { get; set; } // Order can be assigned, but not mandatory
}
}
Will serialize to:
<MyClass MyAttribute="str4">
<ListElms>
<str3>zzz</str3>
<str2>yyy</str2>
<str1>xxx</str1>
</ListElms>
<ListElms>
<str3>zzz</str3>
<str2>yyy</str2>
<str1>xxx</str1>
</ListElms>
<str3>str3</str3>
<!--This is an XML comment-->
<str2>str2</str2>
</MyClass>

Categories

Resources