C# deserialization CBISDDStsRptPhyMsg - c#

I need to deserialize a XML that follows the CBI Italian standard, the problem is that I've already marked every class with the namespace's but I'm still unable to deserialize.
This is part of the xml that I'm trying to deserialize (content erased):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RISPOSTASDD:CBISDDStsRptPhyMsg xmlns:HE2E="urn:CBI:xsd:CBIHdrSrv.001.07" xmlns:BODY="urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00" xmlns:SGNT="urn:CBI:xsd:CBISgnInf.001.04" xmlns:RISPOSTASDD="urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00" xmlns:LMSG="urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00" xmlns:HTRT="urn:CBI:xsd:CBIHdrTrt.001.07">
<RISPOSTASDD:CBIHdrTrt>
<HTRT:IdCBISndrf></HTRT:IdCBISndrf>
<HTRT:IdCBIRcvrf></HTRT:IdCBIRcvrf>
<HTRT:SrvNm></HTRT:SrvNm>
<HTRT:IdMsgTrt></HTRT:IdMsgTrt>
<HTRT:XMLCrtDt></HTRT:XMLCrtDt>
<HTRT:RtrnAddrl></HTRT:RtrnAddrl>
</RISPOSTASDD:CBIHdrTrt>
<RISPOSTASDD:CBIHdrSrv>
<HE2E:SrvInfo>
<HE2E:SrvNm></HE2E:SrvNm>
<HE2E:IdE2EMsg></HE2E:IdE2EMsg>
<HE2E:XMLCrtDt></HE2E:XMLCrtDt>
</HE2E:SrvInfo>
<HE2E:Sender>
<HE2E:IdCBISend></HE2E:IdCBISend>
<HE2E:SendTyp></HE2E:SendTyp>
<HE2E:CBIRefrSend></HE2E:CBIRefrSend>
</HE2E:Sender>
<HE2E:Receiver>
<HE2E:IdCBIRecv></HE2E:IdCBIRecv>
<HE2E:RecvTyp></HE2E:RecvTyp>
<HE2E:CBIRefrRecv></HE2E:CBIRefrRecv>
</HE2E:Receiver>
<HE2E:DiagInfo>
<HE2E:UsrBnk></HE2E:UsrBnk>
<HE2E:DiagVers></HE2E:DiagVers>
<HE2E:ChkSbj></HE2E:ChkSbj>
<HE2E:ChkDt></HE2E:ChkDt>
</HE2E:DiagInfo>
<HE2E:CongrInfo>
<HE2E:SrvBdyNb></HE2E:SrvBdyNb>
</HE2E:CongrInfo>
</RISPOSTASDD:CBIHdrSrv>
<RISPOSTASDD:CBIBdySDDStsRpt>
<BODY:PhyMsgInf>
<BODY:PhyMsgTpCd></BODY:PhyMsgTpCd>
<BODY:NbOfLogMsg></BODY:NbOfLogMsg>
</BODY:PhyMsgInf>
<BODY:CBIEnvelSDDStsRptLogMsg>
<BODY:CBISDDStsRptLogMsg>...
And these are (some) the classes that I've wrote to deserialize it:
[Serializable, XmlRoot(Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public class CBISDDStsRptPhyMsg
{
[XmlElement("CBIHdrTrt", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public CBIHdrTrt CBIHdrTrt {get;set;}
[XmlElement("CBIHdrSrv", Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public CBIHdrSrv CBIHdrSrv {get;set;}
[XmlElement("CBIBdySDDStsRpt", Namespace="urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")]
public CBIBdySDDStsRpt CBIBdySDDStsRpt { get; set; }
}
[Serializable]
public class CBIHdrTrt
{
[XmlElement("IdCBISndrf", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string IdCBISndrf { get; set; }
[XmlElement("IdCBIRcvrf", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string IdCBIRcvrf { get; set; }
[XmlElement("SrvNm", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string SrvNm { get; set; }
[XmlElement("IdMsgTrt", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string IdMsgTrt { get; set; }
[XmlElement("XMLCrtDt", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public DateTime XMLCrtDt { get; set; }
[XmlElement("RtrnAddrl", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")]
public string RtrnAddrl { get; set; }
}
[Serializable]
public class CBIHdrSrv
{
[XmlElement("SrvInfo",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public SrvInfo SrvInfo { get; set; }
[XmlElement("Sender",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public Sender Sender { get; set; }
[XmlElement("Receiver",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public Receiver Receiver { get; set; }
[XmlElement("DiagInfo",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public DiagInfo DiagInfo { get; set; }
[XmlElement("CongrInfo",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public CongrInfo CongrInfo { get; set; }
}
[Serializable]
public class SrvInfo
{
[XmlElement("SrvNm",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string SrvNm { get; set; }
[XmlElement("IdE2EMsg",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string IdE2EMsg { get; set; }
[XmlElement("XMLCrtDt",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public DateTime XMLCrtDt { get; set; }
}
[Serializable]
public class Sender
{
[XmlElement("IdCBISend",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string IdCBISend { get; set; }
[XmlElement("SendTyp",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string SendTyp { get; set; }
[XmlElement("CBIRefrSend",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string CBIRefrSend { get; set; }
}
[Serializable]
public class Receiver
{
[XmlElement("IdCBIRecv",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string IdCBIRecv { get; set; }
[XmlElement("RecvTyp",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string RecvTyp { get; set; }
[XmlElement("CBIRefrRecv",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string CBIRefrRecv { get; set; }
}
[Serializable]
public class DiagInfo
{
[XmlElement("UsrBnk",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string UsrBnk { get; set; }
[XmlElement("DiagVers",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string DiagVers { get; set; }
[XmlElement("ChkSbj",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public string ChkSbj { get; set; }
[XmlElement("ChkDt",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public DateTime ChkDt { get; set; }
}
[Serializable]
public class CongrInfo
{
[XmlElement("SrvBdyNb",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")]
public int SrvBdyNb { get; set; }
}
[Serializable]
public class CBIBdySDDStsRpt
{
[XmlElement("PhyMsgInf", Namespace = "urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")]
public PhyMsgInf PhyMsgInf { get; set; }
[XmlElement("CBIEnvelSDDStsRptLogMsg", Namespace = "urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")]
public CBIEnvelSDDStsRptLogMsg CBIEnvelSDDStsRptLogMsg { get; set; }
}
[Serializable]
public class CBIEnvelSDDStsRptLogMsg
{
[XmlElement("CBISDDStsRptLogMsg", Namespace = "urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")]
public CBISDDStsRptLogMsg CBISDDStsRptLogMsg { get; set; }
}
[Serializable]
public class CBISDDStsRptLogMsg
{
[XmlElement("GrpHdr", Namespace = "urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00")]
public GroupHeader GroupHeader { get; set; }
[XmlElement("OrgnlGrpInfAndSts", Namespace = "urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00")]
public OriginalGroupInformationAndStatus OriginalGroupInformationAndStatus { get; set; }
[XmlElement("OrgnlPmtInfAndSts", Namespace = "urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00")]
public List<OriginalPaymentInformationAndStatus> OriginalPaymentInformationAndStatus { get; set; }
}
My deserialization is pretty simple:
var sITA = new XmlSerializer(typeof(CBISDDStsRptPhyMsg));
var xmlITA = new CBISDDStsRptPhyMsg();
using(var reader = XmlReader.Create(fileInput.InputStream))
{
xmlITA = (CBISDDStsRptPhyMsg)sITA.Deserialize(reader);
}
and at the end of it, all of my 3 main objects are null.
Does anyone have a clue on what I'm doing wrong? I've already managed to serialize/deserialize every SEPA files (pain.00x) that I've found but this Italian standard is giving me quite some head-aches for a while.
Does anyone have the classes that are able to deserialize this italian standard? (Don't bother on finding the .xsd to generate it because I've already googled quite a bit and the .xsd is nowhere to be found!)
Thanks in advance.

The three child elements of the root element in the same namespace as the root, the one prefixed RISPOSTASDD:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RISPOSTASDD:CBISDDStsRptPhyMsg xmlns:HE2E="urn:CBI:xsd:CBIHdrSrv.001.07" xmlns:BODY="urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00" xmlns:SGNT="urn:CBI:xsd:CBISgnInf.001.04" xmlns:RISPOSTASDD="urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00" xmlns:LMSG="urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00" xmlns:HTRT="urn:CBI:xsd:CBIHdrTrt.001.07">
<RISPOSTASDD:CBIHdrTrt>
<!-- Nested elements snipped -->
</RISPOSTASDD:CBIHdrTrt>
<RISPOSTASDD:CBIHdrSrv>
<!-- Nested elements snipped -->
</RISPOSTASDD:CBIHdrSrv>
<RISPOSTASDD:CBIBdySDDStsRpt>
<!-- Remaining XML not included in the question -->
Thus your root class needs to be modified as follows:
[Serializable, XmlRoot(Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public class CBISDDStsRptPhyMsg
{
[XmlElement("CBIHdrTrt", Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public CBIHdrTrt CBIHdrTrt { get; set; }
[XmlElement("CBIHdrSrv", Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public CBIHdrSrv CBIHdrSrv { get; set; }
[XmlElement("CBIBdySDDStsRpt", Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")]
public CBIBdySDDStsRpt CBIBdySDDStsRpt { get; set; }
}
(Or you could just omit the Namespace = on the properties since it's the same as in the XmlRoot attribute.)
There may be other problems, but your question doesn't contain a full mcve (the XML and classes are both incomplete) but at the minimum this looks incorrect.

Related

Xml deserialization returns empty array even though POCO has right xml attributes generated from xsd2code++ tool

I am using XSD2Code++ 2019 tool for visual studio to generate POCOs from aset of 5 xsds. I have added the POCO class below. I see that it has the right xml decorators for it to serialize properly. But I really fail to understand or figure out why the 3rd level object in the returned deserialized data is always empty and not typecasted to the correct type.
I have tried changing attributes to xmlArray and xmlArrayElement too but none of that worked.
POCO class-
https://gist.github.com/nimisha84/b86a4bb2bf37aea6ec351a9f6e331bed
Sample xml response which has null values after deserialization using c# code-
<?xml version="1.0" encoding="UTF-8"?>
<IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2019-07-05T14:29:08.603-07:00">
<QueryResponse startPosition="1" maxResults="1" totalCount="1">
<Invoice domain="QBO" sparse="false">
<Id>8633</Id>
<SyncToken>14</SyncToken>
<MetaData>
<CreateTime>2019-01-09T11:32:12-08:00</CreateTime>
<LastUpdatedTime>2019-06-05T12:49:40-07:00</LastUpdatedTime>
</MetaData>
<CustomField>
<DefinitionId>1</DefinitionId>
<Name>CustomPO</Name>
<Type>StringType</Type>
<StringValue>Gold</StringValue>
</CustomField>
<DocNumber>2830</DocNumber>
<TxnDate>2019-01-09</TxnDate>
<CurrencyRef name="United States Dollar">USD</CurrencyRef>
<ExchangeRate>1</ExchangeRate>
<PrivateNote>Voided - Voided</PrivateNote>
<Line>
<Id>1</Id>
<LineNum>1</LineNum>
<Description>Description</Description>
<Amount>0</Amount>
<DetailType>SalesItemLineDetail</DetailType>
<SalesItemLineDetail>
<ItemRef name="Name27140">815</ItemRef>
<Qty>0</Qty>
<TaxCodeRef>NON</TaxCodeRef>
</SalesItemLineDetail>
</Line>
<Line>
<Amount>0</Amount>
<DetailType>SubTotalLineDetail</DetailType>
<SubTotalLineDetail />
</Line>
<TxnTaxDetail>
<TotalTax>0</TotalTax>
</TxnTaxDetail>
<CustomerRef name="a4">2561</CustomerRef>
<DueDate>2019-01-09</DueDate>
<TotalAmt>0</TotalAmt>
<HomeTotalAmt>0</HomeTotalAmt>
<ApplyTaxAfterDiscount>false</ApplyTaxAfterDiscount>
<PrintStatus>NeedToPrint</PrintStatus>
<EmailStatus>NotSet</EmailStatus>
<Balance>0</Balance>
<Deposit>0</Deposit>
<AllowIPNPayment>false</AllowIPNPayment>
<AllowOnlinePayment>false</AllowOnlinePayment>
<AllowOnlineCreditCardPayment>false</AllowOnlineCreditCardPayment>
<AllowOnlineACHPayment>false</AllowOnlineACHPayment>
</Invoice>
</QueryResponse>
</IntuitResponse>
Code to deserialize-
string responseText = apiResponse.ReadToEnd();
var responseSerializer = new XmlObjectSerializer();
IntuitResponse restResponse =
(IntuitResponse)this.responseSerializer.Deserialize<IntuitResponse>(responseText);
res=restResponse.Items[0] as QueryResponse;
here QueryResponse is not having Invoice(of type IntuitEntity) object returned. Instead empty value is returned. See screenshot.
https://imgur.com/a/5yF6Khb
I really need help to figure out why the 3rd level property is returned as empty.
I tested code below and it works. I manually generated the classes which produces simpler results than using tools.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string responseText = File.ReadAllText(FILENAME);
StringReader reader = new StringReader(responseText);
XmlReader xReader = XmlReader.Create(reader);
XmlSerializer serializer = new XmlSerializer(typeof(IntuitResponse));
IntuitResponse response = (IntuitResponse)serializer.Deserialize(xReader);
}
}
[XmlRoot(ElementName = "IntuitResponse", Namespace = "http://schema.intuit.com/finance/v3")]
public class IntuitResponse
{
[XmlAttribute("time")]
public DateTime time { get; set; }
[XmlElement(ElementName = "QueryResponse", Namespace = "http://schema.intuit.com/finance/v3")]
public QueryResponse response { get; set; }
}
public class QueryResponse
{
[XmlAttribute("startPosition")]
public int startPosition { get; set; }
[XmlAttribute("maxResults")]
public int maxResults { get; set; }
[XmlAttribute("totalCount")]
public int totalCount { get; set; }
[XmlElement(ElementName = "Invoice", Namespace = "http://schema.intuit.com/finance/v3")]
public Invoice invoice { get; set; }
}
public class Invoice
{
[XmlAttribute("domain")]
public string domain { get; set; }
[XmlAttribute("sparse")]
public Boolean sparse { get; set; }
public int Id { get; set; }
public int SyncToken { get; set; }
[XmlElement(ElementName = "MetaData", Namespace = "http://schema.intuit.com/finance/v3")]
public MetaData metaData { get; set; }
[XmlElement(ElementName = "CustomField", Namespace = "http://schema.intuit.com/finance/v3")]
public CustomField customField { get; set; }
public int DocNumber { get; set; }
public DateTime TxnDate { get; set; }
[XmlElement(ElementName = "CurrencyRef", Namespace = "http://schema.intuit.com/finance/v3")]
public CurrencyRef currencyRef { get; set; }
public int ExchangeRate { get; set; }
public string PrivateNote { get; set; }
[XmlElement(ElementName = "Line", Namespace = "http://schema.intuit.com/finance/v3")]
public List<Line> line { get; set; }
[XmlElement(ElementName = "TxnTaxDetail", Namespace = "http://schema.intuit.com/finance/v3")]
public TxnTaxDetail txnTaxDetail { get; set; }
[XmlElement(ElementName = "CustomerRef", Namespace = "http://schema.intuit.com/finance/v3")]
public CustomerRef CustomerRef { get; set; }
public DateTime DueDate { get; set; }
public int TotalAmt { get; set; }
public int HomeTotalAmt { get; set; }
public Boolean ApplyTaxAfterDiscount { get; set; }
public string PrintStatus { get; set; }
public string EmailStatus { get; set; }
public int Balance { get; set; }
public int Deposit { get; set; }
public Boolean AllowIPNPayment { get; set; }
public Boolean AllowOnlinePayment { get; set; }
public Boolean AllowOnlineCreditCardPayment { get; set; }
public Boolean AllowOnlineACHPayment { get; set; }
}
public class MetaData
{
public DateTime CreateTime { get; set; }
public DateTime LastUpdatedTime { get; set; }
}
public class CustomField
{
public int DefinitionId { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string StringValue { get; set; }
}
public class CurrencyRef
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlText]
public string value { get; set; }
}
public class Line
{
public int Id { get; set; }
public int LineNum { get; set; }
public string Description { get; set; }
public decimal Amount { get; set; }
public string DetailType { get; set; }
[XmlElement(ElementName = "SalesItemLineDetail", Namespace = "http://schema.intuit.com/finance/v3")]
public SalesItemLineDetail salesItemLineDetail { get; set; }
}
public class CustomerRef
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlText]
public string value { get; set; }
}
public class SalesItemLineDetail
{
[XmlElement(ElementName = "ItemRef", Namespace = "http://schema.intuit.com/finance/v3")]
public ItemRef itemRef { get; set; }
public int Qty { get; set; }
public string TaxCodeRef { get; set; }
}
public class ItemRef
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlText]
public string value { get; set; }
}
public class TxnTaxDetail
{
public int TotalTax { get; set; }
}
}

How to fix my list of objects being serialized improperly

I have been having an issue to serialize an object properly. I have an object SensorSession which looks like:
[XmlRoot("Session")]
public class SensorSession
{
[XmlElement("SensorDefinitionCollection")]
public SensorDefinitionCollection mCollection { get; set; }
[XmlElement("SensorRunner")]
public SensorRunner mRunner { set; get; }
}
public class SensorDefinitionCollection : IEnumerable<SensorDefinition>
{
[XmlArray("SensorDefinitionCollection")]
[XmlArrayItem("SensorDefinition")]
public List<SensorDefinition> mCollection;
}
public class SensorDefinition : IEquatable<SensorDefinition>, IComparable<SensorDefinition>
{
[XmlElement("StartingWavelength")]
public double startingWavelength { get; set; }
[XmlElement("StoppingWavelength")]
public double stoppingWavelength { get; set; }
[XmlElement("TargetWavelength")]
public double targetWavelength { get; set; }
}
When I serialize I get the following:
<?xml version="1.0" encoding="utf-8"?>
<Session xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SensorDefinitionCollection>
<StartingWavelength>1300</StartingWavelength>
<StoppingWavelength>1400</StoppingWavelength>
<TargetWavelength>1350</TargetWavelength>
</SensorDefinitionCollection>
<SensorDefinitionCollection>
<StartingWavelength>1200</StartingWavelength>
<StoppingWavelength>1300</StoppingWavelength>
<TargetWavelength>1250</TargetWavelength>
</SensorDefinitionCollection>
<SensorRunner>
<SensorConfig>
<SampleCount>5</SampleCount>
<SampleDelay>5</SampleDelay>
<SampleTolerance>5</SampleTolerance>
</SensorConfig>
</SensorRunner>
</Session>
But What I expect to get is this:
<?xml version="1.0" encoding="utf-8"?>
<Session xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SensorDefinitionCollection>
<SensorDefinition>
<StartingWavelength>1200</StartingWavelength>
<StoppingWavelength>1300</StoppingWavelength>
<TargetWavelength>1250</TargetWavelength>
</SensorDefinition>
</SensorDefinitionCollection>
<SensorRunner>
<SensorConfig>
<SampleCount>5</SampleCount>
<SampleDelay>5</SampleDelay>
<SampleTolerance>5</SampleTolerance>
</SensorConfig>
</SensorRunner>
</Session>
How do I allow for the list (SensorDefinitionCollection) to become a single element, and all the SensorDefinitions in that list to fall inside?
UPDATE:
The issue has been resolved with a little refactoring. Rather than having the collection object contain the list, the object inherits List with the definition objects.
[XmlRoot("Session")]
public class SensorSession
{
[XmlArray("SensorDefinitionCollection")]
[XmlArrayItem("SensorDefinition")]
public SensorDefinitionCollection Collection { get; set; }
[XmlElement("SensorRunner")]
public SensorRunner mRunner { set; get; }
}
[Serializable()]
public class SensorDefinitionCollection : List<SensorDefinition>
{
}
public class SensorDefinition : IEquatable<SensorDefinition>, IComparable<SensorDefinition>
{
[XmlElement("StartingWavelength")]
public double startingWavelength { get; set; }
[XmlElement("StoppingWavelength")]
public double stoppingWavelength { get; set; }
[XmlElement("TargetWavelength")]
public double targetWavelength { get; set; }
}
This should help you with that,
[XmlRoot(ElementName = "SensorDefinition")]
public class SensorDefinition
{
[XmlElement(ElementName = "StartingWavelength")]
public int StartingWavelength { get; set; }
[XmlElement(ElementName = "StoppingWavelength")]
public int StoppingWavelength { get; set; }
[XmlElement(ElementName = "TargetWavelength")]
public int TargetWavelength { get; set; }
}
[XmlRoot(ElementName = "SensorDefinitionCollection")]
public class SensorDefinitionCollection
{
[XmlElement(ElementName = "SensorDefinition")]
public List<SensorDefinition> SensorDefinition { get; set; }
}
[XmlRoot(ElementName = "SensorConfig")]
public class SensorConfig
{
[XmlElement(ElementName = "SampleCount")]
public int SampleCount { get; set; }
[XmlElement(ElementName = "SampleDelay")]
public int SampleDelay { get; set; }
[XmlElement(ElementName = "SampleTolerance")]
public int SampleTolerance { get; set; }
}
[XmlRoot(ElementName = "SensorRunner")]
public class SensorRunner
{
[XmlElement(ElementName = "SensorConfig")]
public List<SensorConfig> SensorConfig { get; set; }
}
[XmlRoot(ElementName = "Session")]
public class SensorSession
{
[XmlElement(ElementName = "SensorDefinitionCollection")]
public List<SensorDefinitionCollection> SensorDefinitionCollection { get; set; }
[XmlElement(ElementName = "SensorRunner")]
public List<SensorRunner> SensorRunner { get; set; }
}

How to Serialize to a "Collection with an Attribute" Using the XML Serializer

I have this model:
[XmlArray(ElementName = "Listing")]
[XmlArrayItem(ElementName = "Classification")]
public List<Classification> classifications { get; set; }
[XmlRoot("Listing")]
public class Classification
{
[XmlAttribute("Name")]
public string name { get; set; }
[XmlText]
public string Value { get; set; }
}
That gives me this:
<Listing>
<Classification Name="Location">AsiaPacific</Classification>
</Listing>
How should I modify my class to get this:
<Listing reference = "MyReference">
<Classification Name="Location">AsiaPacific</Classification>
</Listing>
After a few (hundreds) trial and error, I got the result that I need by modifying the model to:
[XmlElement(ElementName = "Listing")]
public ClassificationWrapper classificationWrapper { get; set; }
public class ClassificationWrapper
{
[XmlAttribute("reference")]
public string ref= "MyReference";
[XmlElement("Classification", typeof(Classification))]
public List<Classification> classifications { get; set; }
public ClassificationWrapper() { this.classifications = new List<Classification>(); }
}
public class Classification
{
[XmlAttribute("Name")]
public string name { get; set; }
[XmlText]
public string Value { get; set; }
}

C# XMLNS complicated deserialization

I'm trying to deserialize XMLNS file.
<feed xmlns:live="http://www.live.com/marketplace" xmlns="http://www.w3.org/2005/Atom">
<live:totalItems>1177</live:totalItems>
<live:numItems>1</live:numItems>
<title>FindGames Results</title>
<updated>2013-09-19T09:28:02.77Z</updated>
<entry live:itemNum="34" live:detailView="3">
<id>urn:uuid:66ACD000-77FE-1000-9115-D802555308C3</id>
<updated>2013-09-19T09:28:02.77Z</updated>
<title>Rayman® Legends</title>
<content type="text">game content</content>
<live:media>
<live:mediaType>1</live:mediaType>
<live:gameTitleMediaId>urn:uuid:66ACD000-77FE-1000-9115-D802555308C3</live:gameTitleMediaId>
<live:reducedTitle>Rayman® Legends</live:reducedTitle>
<live:reducedDescription>The Glade of Dreams is in trouble once again! During a 100-year nap, nightmares have multiplied and spread, creating new monsters even scarier than before!</live:reducedDescription>
<live:availabilityDate>2013-06-11T00:00:00</live:availabilityDate>
<live:releaseDate>2013-08-29T00:00:00</live:releaseDate>
<live:ratingId>20</live:ratingId>
<live:developer>Ubisoft</live:developer>
<live:publisher>Ubisoft</live:publisher>
<live:newestOfferStartDate>2013-09-13T09:00:00Z</live:newestOfferStartDate>
<live:totalOfferCount>1</live:totalOfferCount>
<live:titleId>1431505091</live:titleId>
<live:effectiveTitleId>1431505091</live:effectiveTitleId>
<live:gameReducedTitle>Rayman® Legends</live:gameReducedTitle>
<live:ratingAggregate>4.50</live:ratingAggregate>
<live:numberOfRatings>1193</live:numberOfRatings>
</live:media>
</entry>
</feed>
My current code:
Deserialize class:
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
public string title { get; set; }
public string totalItems { get; set; }
public string reducedDescription{ get; set; }
public string ratingId { get; set; }
public string developer { get; set; }
public string publisher { get; set; }
public string tittleId { get; set; }
public string ratingAggregate { get; set; }
public string numberOfRatings { get; set; }
public string boxImage { get; set; }
public string categories { get; set; }
}
class deserializeXML
{
public static void deserialize()
{
using (StreamReader reader = new StreamReader("Query.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(entry));
entry x1 = serializer.Deserialize(reader) as entry;
}
}
}
I just receiving title (FindGames Results instead of Rayman® Legends).
I need to get parameters after "entry" and "live:media", i have no idea how to receive parameters with "live:" at the beginning.
EDIT:
[XmlElement(Namespace = "http://www.live.com/marketplace")]
public int numItems { get; set; }
[XmlElement(Namespace = "http://www.live.com/marketplace")]
public int totalItems { get; set; }
It's working well, but:
[XmlElement("media" Namespace="http://www.live.com/marketplace")]
public media media{ get; set; }
This returning null media class :/
You need to explicitly map those types and properties that are not in the atom namespace to their respective namespaces. The namespace declaration in the XmlRoot just specifies the namespace of the root element. If you have multiple namespaces in the document you need to call them out explicitly.
For instance to map the itemNum attribute correctly try this in your entry class:
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
[XmlAttribute("itemNum", Namespace = "http://www.live.com/marketplace")]
public int itemNum { get; set; }
public string title { get; set; }
public string totalItems { get; set; }
public string reducedDescription { get; set; }
public string ratingId { get; set; }
public string developer { get; set; }
public string publisher { get; set; }
public string tittleId { get; set; }
public string ratingAggregate { get; set; }
public string numberOfRatings { get; set; }
public string boxImage { get; set; }
public string categories { get; set; }
}
You then need to declare additional type to map into things like the media node.
class media
{
public int mediaType{ get; set; }
public string reducedDescription{ get; set; }
etc. etc.
}
You then need to move all of those "live" properties from your entry class into you new media class leaving you with an entry class that might look like this:
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
[XmlAttribute("itemNum", Namespace = "http://www.live.com/marketplace")]
public int itemNum { get; set; }
public string title { get; set; }
[XmlElement("media" Namespace="http://www.live.com/marketplace")]
public media media{ get; set; }
}
Your class hierarchy does not need to exactly match the xml hierarchy and you can do some significant remapping and transformation of the Xml using the various attributes in System.Xml.Serialization however I find it easier to implement, understand and maintain this sort of stuff if the C# classes map 1:1 onto the xml structure.

Naked partial class

I'm trying to addsome Data Annotation using a Partial Class.
As you can see I added a Test Property to my Partial Class so I can test if it really match with the other partials
(as following this article http://msdn.microsoft.com/en-us/library/ee256141.aspx)
It seems that my class is a naked partial class so I'm not sure what I'm doing wrong here.
The problem is the MetaData do not apply to the Partial Class (so the Partial Class is ignored)
Could you please help me out? Thanks
using System;
using System.Collections.Generic;
namespace MyProject.Models
{
public partial class ReAdvSlot
{
// Poco
public int AdvSlotId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsPublished { get; set; }
public string Code { get; set; }
public string Notes { get; set; }
}
}
using System.ComponentModel.DataAnnotations;
namespace MyProject.Models
{
[MetadataType(typeof(ReAdvSlotMetaData))]
public partial class ReAdvSlot
{
public class ReAdvSlotMetaData
{
public int AdvSlotId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsPublished { get; set; }
public string Code { get; set; }
public string Notes { get; set; }
public string TestProperty { get; set; } // TEST PROPERTY
}
}
}
The partial class is not ignored. If you were to put Test property into the actual partial class instead of the metadata, you would see it in the class definition.
namespace MyProject.Models
{
public partial class ReAdvSlot
{
public int AdvSlotId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsPublished { get; set; }
public string Code { get; set; }
public string Notes { get; set; }
}
}
}
namespace MyProject.Models
{
[MetadataType(typeof(ReAdvSlotMetaData))]
public partial class ReAdvSlot
{
public string TestProperty { get; set; } // TEST PROPERTY here instead
}
public class ReAdvSlotMetaData
{
[Required] //Example of defining metadata
public int AdvSlotId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsPublished { get; set; }
public string Code { get; set; }
public string Notes { get; set; }
}
}

Categories