I have a c# class, I am creating object to put data in class , some data i have no able to do, here is my code
InvoiceType oInvoiceType = new InvoiceType(); // my main object
// using object UBLVersionIDType
UBLVersionIDType oUBLVersionIDType = new UBLVersionIDType();
IdentifierType oIdentifierType = new IdentifierType();
oIdentifierType.Value = "UBL 2.1";
//oUBLVersionIDType.schemeID = oIdentifierType.schemeID;
oUBLVersionIDType.Value = oIdentifierType.Value;
// using object InvoiceTypeCodeType
InvoiceTypeCodeType oInvoiceTypeCode = new InvoiceTypeCodeType();
CodeType oCodeType = new CodeType();
oCodeType.Value = "01";
oInvoiceTypeCode.Value = oCodeType.Value;
// using Note
NoteType oNoteType = new NoteType();
oNoteType.Value = new
TextType oTextType = new TextType(); // this show error
oTextType.Value = "This is a Note 1"; // this show error
oNoteType.Value = oTextType.Value; // this show error
// asign main object
oInvoiceType.UBLVersionID = oUBLVersionIDType;
oInvoiceType.InvoiceTypeCode = oInvoiceTypeCode;
//oInvoiceType.Note = oNoteType; // this show error !!!!!!!!
Next you can see the class
public partial class InvoiceType
{
private UBLVersionIDType uBLVersionIDField;
private InvoiceTypeCodeType invoiceTypeCodeField;
private UBLExtensionType[] uBLExtensionsField;
private NoteType[] noteField;
public UBLVersionIDType UBLVersionID
{
get
{
return this.uBLVersionIDField;
}
set
{
this.uBLVersionIDField = value;
}
}
public InvoiceTypeCodeType InvoiceTypeCode
{
get
{
return this.invoiceTypeCodeField;
}
set
{
this.invoiceTypeCodeField = value;
}
}
public UBLExtensionType[] UBLExtensions
{
get
{
return this.uBLExtensionsField;
}
set
{
this.uBLExtensionsField = value;
}
}
public NoteType[] Note
{
get
{
return this.noteField;
}
set
{
this.noteField = value;
}
}
}
public partial class NoteType : TextType1
{
}
public partial class TextType1 : TextType
{
}
public partial class TextType
{
private string languageIDField;
private string languageLocaleIDField;
private string valueField;
public string languageID
{
get
{
return this.languageIDField;
}
set
{
this.languageIDField = value;
}
}
public string languageLocaleID
{
get
{
return this.languageLocaleIDField;
}
set
{
this.languageLocaleIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
public partial class UBLExtensionType
{
private IDType idField;
private NameType1 nameField;
private System.Xml.XmlElement extensionContentField;
public IDType ID
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
public NameType1 Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
public System.Xml.XmlElement ExtensionContent
{
get
{
return this.extensionContentField;
}
set
{
this.extensionContentField = value;
}
}
}
The class "Invoice" has 4 fields to set data
private UBLVersionIDType uBLVersionIDField;
private InvoiceTypeCodeType invoiceTypeCodeField;
private UBLExtensionType[] uBLExtensionsField;
private NoteType[] noteField;
As you can see in the code, I set data to "uBLVersionID" and "InvoiceTypeCode", but I have not been able to assign data to notefield, and I don't know to set "uBLExtensionfield".
Does someone know how to do that?
Code below compiles with no errors. I added missing classes. The line with the ******* is causing issues.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
InvoiceType oInvoiceType = new InvoiceType(); // my main object
// using object UBLVersionIDType
UBLVersionIDType oUBLVersionIDType = new UBLVersionIDType();
IdentifierType oIdentifierType = new IdentifierType();
oIdentifierType.Value = "UBL 2.1";
//oUBLVersionIDType.schemeID = oIdentifierType.schemeID;
oUBLVersionIDType.Value = oIdentifierType.Value;
// using object InvoiceTypeCodeType
InvoiceTypeCodeType oInvoiceTypeCode = new InvoiceTypeCodeType();
CodeType oCodeType = new CodeType();
oCodeType.Value = "01";
oInvoiceTypeCode.Value = oCodeType.Value;
// using Note
NoteType oNoteType = new NoteType();
//oNoteType.Value = new ******************************************** bad
TextType oTextType = new TextType(); // this show error
oTextType.Value = "This is a Note 1"; // this show error
oNoteType.Value = oTextType.Value; // this show error
// asign main object
oInvoiceType.UBLVersionID = oUBLVersionIDType;
oInvoiceType.InvoiceTypeCode = oInvoiceTypeCode;
//oInvoiceType.Note = oNoteType; // this show error !!!!!!!!
}
}
public class CodeType
{
public string Value { get; set; }
}
public class IdentifierType
{
public string Value { get;set;}
}
public class UBLVersionIDType
{
public string Value { get; set; }
}
public class InvoiceTypeCodeType
{
public string Value { get; set; }
}
public partial class InvoiceType
{
private UBLVersionIDType uBLVersionIDField;
private InvoiceTypeCodeType invoiceTypeCodeField;
private UBLExtensionType[] uBLExtensionsField;
private NoteType[] noteField;
public UBLVersionIDType UBLVersionID
{
get
{
return this.uBLVersionIDField;
}
set
{
this.uBLVersionIDField = value;
}
}
public InvoiceTypeCodeType InvoiceTypeCode
{
get
{
return this.invoiceTypeCodeField;
}
set
{
this.invoiceTypeCodeField = value;
}
}
public UBLExtensionType[] UBLExtensions
{
get
{
return this.uBLExtensionsField;
}
set
{
this.uBLExtensionsField = value;
}
}
public NoteType[] Note
{
get
{
return this.noteField;
}
set
{
this.noteField = value;
}
}
}
public partial class NoteType : TextType1
{
}
public partial class TextType1 : TextType
{
}
public partial class TextType
{
private string languageIDField;
private string languageLocaleIDField;
private string valueField;
public string languageID
{
get
{
return this.languageIDField;
}
set
{
this.languageIDField = value;
}
}
public string languageLocaleID
{
get
{
return this.languageLocaleIDField;
}
set
{
this.languageLocaleIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
public class IDType
{
}
public class NameType1
{
}
public partial class UBLExtensionType
{
private IDType idField;
private NameType1 nameField;
private System.Xml.XmlElement extensionContentField;
public IDType ID
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
public NameType1 Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
public System.Xml.XmlElement ExtensionContent
{
get
{
return this.extensionContentField;
}
set
{
this.extensionContentField = value;
}
}
}
}
I solved myself, This is the REAL answer and its working fine
NoteType[] arrayNote = new NoteType[3];
NoteType oObj1 = new NoteType();
TextType oTextType = new TextType();
oTextType.Value = "This is Note 1";
oObj1.Value = oTextType.Value;
arrayNote[0] = oObj1;
oObj1.Value = "This is Note 2";
arrayNote[1] = oObj1;
Related
Below is the portion of code from Propertyware API that is consumed.
public OwnerLedger appendOwnerLedgerItems(OwnerLedger ownerLedger, Owner owner) {
object[] results = this.Invoke("appendOwnerLedgerItems", new object[] {
ownerLedger,
owner});
return ((OwnerLedger)(results[0]));
}
public partial class OwnerLedger : Ledger {
}
public abstract partial class Ledger : ClientDataContainer {
private LedgerItem[] itemsField;
/// <remarks/>
[System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
public LedgerItem[] items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
public abstract partial class LedgerItem : FinancialTransaction {
}
public abstract partial class OwnerLedgerItem : LedgerItem {
}
public partial class OwnerContribution : OwnerLedgerItem {
private string commentsField;
private System.Nullable<System.DateTime> dateField;
private string paymentTypeField;
private string referenceNumberField;
/// <remarks/>
[System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
public string comments {
get {
return this.commentsField;
}
set {
this.commentsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
public System.Nullable<System.DateTime> date {
get {
return this.dateField;
}
set {
this.dateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
public string paymentType {
get {
return this.paymentTypeField;
}
set {
this.paymentTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.SoapElementAttribute(IsNullable=true)]
public string referenceNumber {
get {
return this.referenceNumberField;
}
set {
this.referenceNumberField = value;
}
}
}
In the above code i need to use the "appendOwnerLedgerItems" method to create an owner contribution entry in Propertyware. For that i try to use the below logic but it fails. The error message is "java.lang.ClassCastException: [Lcom.realpage.propertyware.web.service.soap.AbstractLedgerItemDTO; cannot be cast to [Lcom.realpage.propertyware.web.service.soap.AbstractOwnerLedgerItemDTO;"
OwnerContribution oc = new OwnerContribution();
oc.amount = 10;
oc.comments = "Test Entry";
oc.date = System.DateTime.Now;
oc.paymentType = "Check";
oc.referenceNumber = "12345";
Owner ow = new Owner();
ow.ID = 12345;
LedgerItem[] li = new LedgerItem[1];
li[0] = oc;
OwnerLedger owl = new OwnerLedger();
owl.items = li;
OwnerLedger owl1 = client.appendOwnerLedgerItems(owl,ow); // This is where i get the cast error
How to solve this issue?
I don't know much about Propertyware but can you try this, basically change the order of objects passed to the invoke method:
public OwnerLedger appendOwnerLedgerItems(OwnerLedger ownerLedger, Owner owner) {
object[] results = this.Invoke("appendOwnerLedgerItems", new object[] {
owner, ownerLedger});
return ((OwnerLedger)(results[0]));
}
I have converted the following two classes to c# from vb.net, but get a reference error. Can someone please help or explain why it does not work in c# but does in vb.net?
Member class:
public class Member
{
#region "Fields"
private string fPiecemark;
private string fMemberType;
private string fSize;
private string fTotalWeight;
private int fSheetKey;
private string fDescription;
private string fStructType;
#endregion
private string fMemberSheetIndex;
#region "Constructors"
//Default class Constructor
public Member()
{
fPiecemark = string.Empty;
fMemberType = string.Empty;
fSize = string.Empty;
fTotalWeight = string.Empty;
fSheetKey = 0;
fStructType = string.Empty;
}
public Member(string Piecemark, string MemberType, string Description, string Size, string TotalWeight, string StructType, string MemberSheetIndex, int SheetID)
{
this.Piecemark = Piecemark;
this.MemberType = MemberType;
this.Description = Description;
this.Size = Size;
this.TotalWeight = TotalWeight;
this.StructType = StructType;
this.MemberSheetIndex = MemberSheetIndex;
this.SheetKey = SheetID;
if (!MbrSheet.mSheet.ContainsKey(SheetID))
{
MbrSheet.mSheet.Add(SheetID, new MbrSheet(SheetID));
}
MbrSheet.mSheets[SheetID].Members.Add(this);
}
#endregion
#region "Properties"
public string Piecemark
{
get { return fPiecemark; }
set { fPiecemark = value; }
}
public string MemberType
{
get { return fMemberType; }
set { fMemberType = value; }
}
public string TotalWeight
{
get { return fTotalWeight; }
set { fTotalWeight = value; }
}
public string Size
{
get { return fSize; }
set { fSize = value; }
}
public int SheetKey
{
get { return fSheetKey; }
set { fSheetKey = value; }
}
public string Description
{
get { return fDescription; }
set { fDescription = value; }
}
public string StructType
{
get { return fStructType; }
set { fStructType = value; }
}
public string MemberSheetIndex
{
get { return fMemberSheetIndex; }
set { fMemberSheetIndex = value; }
}
#endregion
}
MbrSheet class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
public class MbrSheet
{
public static Dictionary<int, MbrSheet> mSheets = new Dictionary<int, MbrSheet>();
public int mSheet { get; set; }
public List<Member> Members { get; set; }
public MbrSheet(int MbrSheet)
{
Members = new List<Member>();
this.mSheet = MbrSheet;
}
public static decimal WeightByType(string MemberType)
{
var subset = mSheets.Where(kvp => kvp.Value.Members.Where(m => m.MemberType == MemberType).Count() > 0);
decimal wbt = 0;
wbt += mSheets
.Where(kvp => kvp.Value.Members.Where(m => m.MemberType == MemberType).Count() > 0)
.Sum(kvp => kvp.Value.Members.Sum(m => Convert.ToDecimal(m.TotalWeight, CultureInfo.InvariantCulture)));
return wbt;
}
}
I get error but don't know why
An object reference is required for the non-static field, method, or property for MbrSheet.mSheet, but both worked in VB.net
if (!MbrSheet.mSheet.ContainsKey(SheetID)) // Error on !MbrSheet.mSheet
{
MbrSheet.mSheet.Add(SheetID, new MbrSheet(SheetID)); // Error on MbrSheet.mSheet
}
I think you should use this:
if (!MbrSheet.mSheets.ContainsKey(SheetID))
{
MbrSheet.mSheets.Add(SheetID, new MbrSheet(SheetID));
}
Pay attention to mSheets you are using mSheet.
You can also use tools to convert codes:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
http://codeconverter.sharpdevelop.net/SnippetConverter.aspx
I have 3 Classes : Masina (Car), Destinatie (Destination) and MasinaDestinatie (CarDestination).
I need the third class to get the values of the car number _nr_masina and the destination _cod_dest through it's own constructor.
I need to make a constructor with parameters in the third class that stores the values of _nr_masina and _cod_dest.
Anyone know how can i do this exactly? I've tried making the private fields public and putting them as parameters but that doesn't work...
The classes:
namespace Problema_test
{
class Masina
{
private string _nr_masina = string.Empty;
private string _valoare = string.Empty;
private string _an_fabricatie = string.Empty;
public Masina(string nr_masina,string valoare, string an_fabricatie)
{
_nr_masina = nr_masina;
_valoare = valoare;
_an_fabricatie = an_fabricatie;
}
public string Numar
{
get { return _nr_masina; }
set { _nr_masina = value; }
}
public string Valoare
{
get { return _valoare; }
set { _valoare = value; }
}
public string Anul
{
get { return _an_fabricatie; }
set { _an_fabricatie = value; }
}
}
class Destinatie
{
private string _cod_destinatie = string.Empty;
private string _adresa = string.Empty;
public Destinatie(string cod_destinatie, string adresa)
{
_cod_destinatie = cod_destinatie;
_adresa = adresa;
}
public string CodDest
{
get { return _cod_destinatie; }
set { _cod_destinatie = value; }
}
public string Adresa
{
get { return _adresa; }
set { _adresa = value; }
}
}
class MasinaDestinatie
{
// how can i make this work?
public MasinaDestinatie(string numarMasina, string codDest)
{
}
}
}
You can store the values inside properties
class MasinaDestinatie
{
public string Numar {get;set;}
public string CodDest {get;set;}
public MasinaDestinatie(string numarMasina, string codDest)
{
Numar = numarMasina;
CodDest = codDest;
}
}
To use the class you have do something like this
var masina = new Masina("Dacia","2000","1992");
var destinatie = new Destinatie("123", "Romania");
var masinaDestinatie = new MasinaDestinatie(masina.Numar, destinatie.CodDest);
Solution 2: As #blas-soriano sugested you can store the reference of the objects (Masina, Destinatie), this way you won't have problems (i.e. CodDest exist only in MasinaDestinatie but not in Destinatie, and many others).
class MasinaDestinatie
{
private Masina _masina {get;set;}
private Destinatie _destinatie {get;set;}
public string Numar { get { return _masina.Numar; } }
public string CodDest { get { return _destinatie.CodDest; } }
public MasinaDestinatie(Masina masina, Destinatie destinatie)
{
_masina = masina;
_destinatie = destinatie;
}
}
To use the class you have do something like this
var masina = new Masina("Dacia","2000","1992");
var destinatie = new Destinatie("123", "Romania");
var masinaDestinatie = new MasinaDestinatie(masina, destinatie);
I am trying to deserialize XML returned from an API call but am getting "InnerException:When converting a string to DateTime. parse the string to date the date before putting each variable into the DateTime object"
The XML looks like this
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<agentInventory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:N1="demo.org.uk/demo/CustomsStatus" xmlns:N2="demo.org.uk/demo/UnLocation" xmlns:N3="demo.org.uk/demo/AirCarrier" xmlns="demo.org.uk/demo/AgentInventory">
<shed>ADX</shed>
<arrivalPort>
<N2:iataPortCode>LHR</N2:iataPortCode>
</arrivalPort>
<masterAirwayBillPrefix>125</masterAirwayBillPrefix>
<masterAirwayBillNumber>28121101</masterAirwayBillNumber>
<nominatedAgent>DRB</nominatedAgent>
<originPort>
<N2:iataPortCode>BOS</N2:iataPortCode>
</originPort>
<destinationPort>
<N2:iataPortCode>LHR</N2:iataPortCode>
</destinationPort>
<airCarrier>
<N3:carrierCode>BA</N3:carrierCode>
</airCarrier>
<flightNumber>235</flightNumber>
<flightDate>2012-02-09T00:00:00Z</flightDate>
<npx>10</npx>
<npr>0</npr>
<grossWeight>123.0</grossWeight>
<goodsDescription>BOOKS</goodsDescription>
<sdc>T</sdc>
<status1Set></status1Set>
<status2Set>false</status2Set>
<ccsCreationTime></ccsCreationTime>
<customsSummaryText />
<customsSummaryTime></customsSummaryTime>
<agentReference />
<isErtsPreArrival>false</isErtsPreArrival>
<isAgentPreArrival>false</isAgentPreArrival>
<isDeleted>false</isDeleted>
<finalised></finalised>
<createdOn>2012-01-24T11:50:40.86Z</createdOn>
<modifiedOn>2012-02-09T09:51:26.617Z</modifiedOn>
</agentInventory>
The code used to deserialize the XML is
if (storeXmlInventoryReturnData != null)
{
//DeSerialize XML from storeXmlInventoryReturnData variable
agentInventory myInventoryResponse = null;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(agentInventory));
myInventoryResponse = (
agentInventory)xmlSerializer.Deserialize(new StringReader(storeXmlInventoryReturnData)
);
Console.WriteLine(
#"\n\n\n INVENTORY RETURN DATA FOR AWB: {0}-{1} \n\n
Destination Port: {2} \n
Arrival Port: {3} \n
Carrier: {4} \n
Flight No: {5} \n
Flight Date: {6} \n
Customers Status: {7} \n
NPX: {8} \n
NPR {9} \n
SDC Code: {10}
\n\n Hit any key to exit...."
,
myInventoryResponse.masterAirwayBillPrefix,
myInventoryResponse.masterAirwayBillNumber,
myInventoryResponse.destinationPort,
myInventoryResponse.arrivalPort,
myInventoryResponse.airCarrier,
myInventoryResponse.flightNumber,
myInventoryResponse.flightDate,
myInventoryResponse.customsStatus,
myInventoryResponse.npx,
myInventoryResponse.npr,
myInventoryResponse.sdc,
myInventoryResponse.grossWeight,
myInventoryResponse.goodsDescription
);
Console.ReadLine();
}
else
{
Console.Write("No data returned");
}
}
The exception is thrown at;
myInventoryResponse = (
agentInventory)xmlSerializer.Deserialize(new StringReader(storeXmlInventoryReturnData));
I guess I need to convert the myInventoryResponse.flightDate to a DateTime object but I am at a loss how to achieve this?
namespace FreightSolutions {
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.Collections.Generic;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="asm.org.uk/Sequoia/AgentInventory")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="demo.org.uk/demo/AgentInventory", IsNullable=false)]
public partial class agentInventory {
private string shedField;
private unLocation arrivalPortField;
private string masterAirwayBillPrefixField;
private string masterAirwayBillNumberField;
private string houseAirwayBillNumberField;
private string splitReferenceNumberField;
private string nominatedAgentField;
private unLocation originPortField;
private unLocation destinationPortField;
private airCarrier airCarrierField;
private string flightNumberField;
private System.DateTime flightDateField;
private bool flightDateFieldSpecified;
private string npxField;
private string nprField;
private float grossWeightField;
private bool grossWeightFieldSpecified;
private string goodsDescriptionField;
private string sdcField;
private System.DateTime status1SetField;
private bool status1SetFieldSpecified;
private bool status2SetField;
private bool status2SetFieldSpecified;
private System.DateTime ccsCreationTimeField;
private bool ccsCreationTimeFieldSpecified;
private customsStatus customsStatusField;
private string customsSummaryTextField;
private System.DateTime customsSummaryTimeField;
private bool customsSummaryTimeFieldSpecified;
private string agentReferenceField;
private bool isErtsPreArrivalField;
private bool isErtsPreArrivalFieldSpecified;
private bool isAgentPreArrivalField;
private bool isAgentPreArrivalFieldSpecified;
private bool isDeletedField;
private bool isDeletedFieldSpecified;
private System.DateTime finalisedField;
private bool finalisedFieldSpecified;
private System.DateTime createdOnField;
private System.DateTime modifiedOnField;
private bool modifiedOnFieldSpecified;
public agentInventory() {
this.customsStatusField = new customsStatus();
this.airCarrierField = new airCarrier();
this.destinationPortField = new unLocation();
this.originPortField = new unLocation();
this.arrivalPortField = new unLocation();
}
public string shed {
get {
return this.shedField;
}
set {
this.shedField = value;
}
}
public unLocation arrivalPort {
get {
return this.arrivalPortField;
}
set {
this.arrivalPortField = value;
}
}
public string masterAirwayBillPrefix {
get {
return this.masterAirwayBillPrefixField;
}
set {
this.masterAirwayBillPrefixField = value;
}
}
public string masterAirwayBillNumber {
get {
return this.masterAirwayBillNumberField;
}
set {
this.masterAirwayBillNumberField = value;
}
}
public string houseAirwayBillNumber {
get {
return this.houseAirwayBillNumberField;
}
set {
this.houseAirwayBillNumberField = value;
}
}
public string splitReferenceNumber {
get {
return this.splitReferenceNumberField;
}
set {
this.splitReferenceNumberField = value;
}
}
public string nominatedAgent {
get {
return this.nominatedAgentField;
}
set {
this.nominatedAgentField = value;
}
}
public unLocation originPort {
get {
return this.originPortField;
}
set {
this.originPortField = value;
}
}
public unLocation destinationPort {
get {
return this.destinationPortField;
}
set {
this.destinationPortField = value;
}
}
public airCarrier airCarrier {
get {
return this.airCarrierField;
}
set {
this.airCarrierField = value;
}
}
public string flightNumber {
get {
return this.flightNumberField;
}
set {
this.flightNumberField = value;
}
}
public System.DateTime flightDate {
get {
return this.flightDateField;
}
set {
this.flightDateField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool flightDateSpecified {
get {
return this.flightDateFieldSpecified;
}
set {
this.flightDateFieldSpecified = value;
}
}
[System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
public string npx {
get {
return this.npxField;
}
set {
this.npxField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
public string npr {
get {
return this.nprField;
}
set {
this.nprField = value;
}
}
public float grossWeight {
get {
return this.grossWeightField;
}
set {
this.grossWeightField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool grossWeightSpecified {
get {
return this.grossWeightFieldSpecified;
}
set {
this.grossWeightFieldSpecified = value;
}
}
public string goodsDescription {
get {
return this.goodsDescriptionField;
}
set {
this.goodsDescriptionField = value;
}
}
public string sdc {
get {
return this.sdcField;
}
set {
this.sdcField = value;
}
}
public System.DateTime status1Set {
get {
return this.status1SetField;
}
set {
this.status1SetField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool status1SetSpecified {
get {
return this.status1SetFieldSpecified;
}
set {
this.status1SetFieldSpecified = value;
}
}
public bool status2Set {
get {
return this.status2SetField;
}
set {
this.status2SetField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool status2SetSpecified {
get {
return this.status2SetFieldSpecified;
}
set {
this.status2SetFieldSpecified = value;
}
}
public System.DateTime ccsCreationTime {
get {
return this.ccsCreationTimeField;
}
set {
this.ccsCreationTimeField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool ccsCreationTimeSpecified {
get {
return this.ccsCreationTimeFieldSpecified;
}
set {
this.ccsCreationTimeFieldSpecified = value;
}
}
public customsStatus customsStatus {
get {
return this.customsStatusField;
}
set {
this.customsStatusField = value;
}
}
public string customsSummaryText {
get {
return this.customsSummaryTextField;
}
set {
this.customsSummaryTextField = value;
}
}
public System.DateTime customsSummaryTime {
get {
return this.customsSummaryTimeField;
}
set {
this.customsSummaryTimeField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool customsSummaryTimeSpecified {
get {
return this.customsSummaryTimeFieldSpecified;
}
set {
this.customsSummaryTimeFieldSpecified = value;
}
}
public string agentReference {
get {
return this.agentReferenceField;
}
set {
this.agentReferenceField = value;
}
}
public bool isErtsPreArrival {
get {
return this.isErtsPreArrivalField;
}
set {
this.isErtsPreArrivalField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool isErtsPreArrivalSpecified {
get {
return this.isErtsPreArrivalFieldSpecified;
}
set {
this.isErtsPreArrivalFieldSpecified = value;
}
}
public bool isAgentPreArrival {
get {
return this.isAgentPreArrivalField;
}
set {
this.isAgentPreArrivalField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool isAgentPreArrivalSpecified {
get {
return this.isAgentPreArrivalFieldSpecified;
}
set {
this.isAgentPreArrivalFieldSpecified = value;
}
}
public bool isDeleted {
get {
return this.isDeletedField;
}
set {
this.isDeletedField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool isDeletedSpecified {
get {
return this.isDeletedFieldSpecified;
}
set {
this.isDeletedFieldSpecified = value;
}
}
public System.DateTime finalised {
get {
return this.finalisedField;
}
set {
this.finalisedField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool finalisedSpecified {
get {
return this.finalisedFieldSpecified;
}
set {
this.finalisedFieldSpecified = value;
}
}
public System.DateTime createdOn {
get {
return this.createdOnField;
}
set {
this.createdOnField = value;
}
}
public System.DateTime modifiedOn {
get {
return this.modifiedOnField;
}
set {
this.modifiedOnField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool modifiedOnSpecified {
get {
return this.modifiedOnFieldSpecified;
}
set {
this.modifiedOnFieldSpecified = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="demo.org.uk/demo/UnLocation")]
public partial class unLocation {
private string itemField;
private ItemChoiceType itemElementNameField;
[System.Xml.Serialization.XmlElementAttribute("iataPortCode", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("oceanPortCode", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
public string Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName {
get {
return this.itemElementNameField;
}
set {
this.itemElementNameField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="demo.org.uk/demo/UnLocation", IncludeInSchema=false)]
public enum ItemChoiceType {
/// <remarks/>
iataPortCode,
/// <remarks/>
oceanPortCode,
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="demo.org.uk/demo/CustomsStatus")]
public partial class customsStatus {
private string codeField;
private string statusTextField;
public string code {
get {
return this.codeField;
}
set {
this.codeField = value;
}
}
public string statusText {
get {
return this.statusTextField;
}
set {
this.statusTextField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="demo.org.uk/demo/AirCarrier")]
public partial class airCarrier {
private string carrierCodeField;
public string carrierCode {
get {
return this.carrierCodeField;
}
set {
this.carrierCodeField = value;
}
}
}
}
For my own sanity sake, I tried getting the provided XML to work with your classes and the nullable DateTime fields.
For them to work, replace the below properties with those linked below.
The default value for a DateTime object is DateTime.MinDate, so if the incoming value is null or empty, then the property will have a value of DateTime.MinDate anyway, for this you might need to additional validation/changes if your code needs to cater for this.
#region XML Nullable helper properties
[XmlElement(ElementName = "status1Set")]
public object status1SetXml
{
get
{
// Return the main property itself here and not the field to ensure that if there are changes made to the getter,
// the resulting XML will always have the right value
return status1Set;
}
set
{
// value passed in is of type XmlNode[], so convert and get the XmlNode text, which should be the DateTime we want
// !!ASSUMPTION!! That it will alwas be XmlNode[1] type
XmlNode[] inputValue = value as XmlNode[];
if (inputValue != null)
{
DateTime.TryParse(Convert.ToString(inputValue[0].InnerText), out status1SetField);
}
}
}
[XmlElement(ElementName="ccsCreationTime")]
public object ccsCreationTimeXml
{
get
{
// Return the main property itself here and not the field to ensure that if there are changes made to the getter,
// the resulting XML will always have the right value
return ccsCreationTime;
}
set
{
// value passed in is of type XmlNode[], so convert and get the XmlNode text, which should be the DateTime we want
// !!ASSUMPTION!! That it will alwas be XmlNode[1] type
XmlNode[] inputValue = value as XmlNode[];
if (inputValue != null)
{
DateTime.TryParse(Convert.ToString(inputValue[0].InnerText), out ccsCreationTimeField);
}
}
}
[XmlElement(ElementName = "customsSummaryTime")]
public object customsSummaryTimeXml
{
get
{
// Return the main property itself here and not the field to ensure that if there are changes made to the getter,
// the resulting XML will always have the right value
return customsSummaryTime;
}
set
{
// value passed in is of type XmlNode[], so convert and get the XmlNode text, which should be the DateTime we want
// !!ASSUMPTION!! That it will alwas be XmlNode[1] type
XmlNode[] inputValue = value as XmlNode[];
if (inputValue != null)
{
DateTime.TryParse(Convert.ToString(inputValue[0].InnerText), out customsSummaryTimeField);
}
}
}
[XmlElement(ElementName = "finalised")]
public object finalisedXml
{
get
{
// Return the main property itself here and not the field to ensure that if there are changes made to the getter,
// the resulting XML will always have the right value
return finalised;
}
set
{
// value passed in is of type XmlNode[], so convert and get the XmlNode text, which should be the DateTime we want
// !!ASSUMPTION!! That it will alwas be XmlNode[1] type
XmlNode[] inputValue = value as XmlNode[];
if (inputValue != null)
{
DateTime.TryParse(Convert.ToString(inputValue[0].InnerText), out finalisedField);
}
}
}
#endregion
#region Non XML properties
[XmlIgnore]
public System.DateTime status1Set
{
get { return this.status1SetField; }
set { this.status1SetField = value; }
}
[XmlIgnore]
public System.DateTime ccsCreationTime
{
get { return this.ccsCreationTimeField; }
set { this.ccsCreationTimeField = value; }
}
[XmlIgnore]
public System.DateTime customsSummaryTime
{
get { return this.customsSummaryTimeField; }
set { this.customsSummaryTimeField = value; }
}
[XmlIgnore]
public System.DateTime finalised
{
get { return this.finalisedField; }
set { this.finalisedField = value; }
}
#endregion
To transfer data, I'm using XmlSerializer. But I get a runtime error at following line:
XmlSerializer serializer = new XmlSerializer(typeof(Packets.Wrapper));
The error is "Additional information: Error reflecting type 'Packets.Wrapper'.". MSDN says that I have to use an empty contructor, but it doesn't fix the error.
The class looks like this:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Packets
{
[Serializable]
public class Wrapper
{
int _sID = 0;
int _sSession = 0;
PacketType _sType = PacketType.None;
AuthRequest _sAuthRequest = null;
AuthResponse _sAuthResponse = null;
ProxyRequest _sProxyRequest = null;
ProxyResponse _sProxyResponse = null;
public Wrapper()
{
}
public int ID
{
get { return _sID; }
set { _sID = value; }
}
public int Session
{
get { return _sSession; }
set { _sSession = value; }
}
public PacketType Type
{
get { return _sType; }
set { _sType = value; }
}
public AuthRequest AuthRequest
{
get { return _sAuthRequest; }
set { _sAuthRequest = value; }
}
public AuthResponse AuthResponse
{
get { return _sAuthResponse; }
set { _sAuthResponse = value; }
}
public ProxyRequest ProxyRequest
{
get { return _sProxyRequest; }
set { _sProxyRequest = value; }
}
public ProxyResponse ProxyResponse
{
get { return _sProxyResponse; }
set { _sProxyResponse = value; }
}
}
[Serializable]
public enum PacketType
{
AuthRequest,
AuthResponse,
ProxyRequest,
ProxyResponse,
None
}
[Serializable]
public enum AuthResult
{
Accepted,
Denied,
Error
}
[Serializable]
public enum ProxyAction
{
Send,
Response,
Connect,
Close
}
[Serializable]
public enum ProxyResult
{
Connected,
Sent,
Revieved,
Error
}
[Serializable]
public class AuthRequest
{
string username = null;
string password = null;
public AuthRequest()
{
}
public string Username
{
get { return username; }
set { username = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
}
[Serializable]
public class AuthResponse
{
AuthResult authResult = AuthResult.Denied;
public AuthResponse()
{
}
public AuthResult AuthResult
{
get { return authResult; }
set { authResult = value; }
}
}
[Serializable]
public class ProxyRequest
{
ProxyAction _sAction;
string _sIP = null;
int _sPort = 0;
TcpClient _sClient = null;
public ProxyRequest()
{
}
public ProxyAction Action
{
get { return _sAction; }
set { _sAction = value; }
}
public string IP
{
get { return _sIP; }
set { _sIP = value; }
}
public int Port
{
get { return _sPort; }
set { _sPort = value; }
}
public TcpClient Client
{
get { return _sClient; }
set { _sClient = value; }
}
}
[Serializable]
public class ProxyResponse
{
public ProxyResult _sResult = ProxyResult.Error;
public StringBuilder _sError = new StringBuilder();
public StringBuilder _sRecieved = new StringBuilder();
public TcpClient _sClient = null;
public ProxyResponse()
{
}
public ProxyResult Result
{
get { return _sResult; }
set { _sResult = value; }
}
public StringBuilder Error
{
get { return _sError; }
set { _sError = value; }
}
public StringBuilder Recieved
{
get { return _sRecieved; }
set { _sRecieved = value; }
}
public TcpClient Client
{
get { return _sClient; }
set { _sClient = value; }
}
}
}
I hope you can help me and thank you for your time.
I got it.
TcpClient isn't serializable.