XmlSerializer Deserialize returns empty array - c#

I'm trying to deserialize the following XML (excerpt):
<NSArray>
<Song id="23507" type="Song">
<title>Waking The Demon</title>
<artist id="17" type="Artist">
<nameWithoutThePrefix>Bullet For My Valentine</nameWithoutThePrefix>
<useThePrefix>false</useThePrefix>
<name>Bullet For My Valentine</name>
</artist>
</Song>
<Song id="3663" type="Song">
<title>Hand Of Blood</title>
<artist id="17" type="Artist"/>
</Song>
<Song id="59226" type="Song">
<title>Your Betrayal</title>
<artist id="17" type="Artist"/>
</Song>
</NSArray>
with the following classes:
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "NSArray", Namespace = "", IsNullable = false)]
public class SearchResult
{
[XmlElement("Song", Form = XmlSchemaForm.Unqualified)]
public Song[] Items { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class Song
{
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string Title { get; set; }
[XmlElement("artist", Form = XmlSchemaForm.Unqualified)]
public Artist Artist { get; set; }
[XmlAttribute]
public string Type { get; set; }
[XmlAttribute]
public string Id { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
public class Artist
{
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string NameWithoutThePrefix { get; set; }
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string UseThePrefix { get; set; }
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string Name { get; set; }
[XmlAttribute]
public string Type { get; set; }
[XmlAttribute]
public string Id { get; set; }
}
and the following code:
var request = WebRequest.Create(string.Format("http://myurl.com");
request.BeginGetResponse(GetEventResponseCallback, request);
private void GetEventResponseCallback(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
var response = request.EndGetResponse(result);
if (response.GetResponseStream() == null) return;
using (var stream = response.GetResponseStream())
{
_xmlReader = XmlReader.Create(stream);
var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;
}
}
However, on var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;, the Deserialization executes successfully, but the songs variable does not contain any data. If I inspect with the debugger, it returns Could not evaluate expression for all the values in the array.
Any hints? Thanks.

Your SearchResult class needs some fixing. You're really close, the code is only missing a few element names and the serializable attributes.
Here's a class that works:
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "NSArray", Namespace = "", IsNullable = false)]
[Serializable]
public class SearchResult
{
[XmlElement("Song", Form = XmlSchemaForm.Unqualified)]
public Song[] Items { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName="Song", Namespace = "", IsNullable = false)]
[Serializable]
public class Song
{
[XmlElement("title", Form = XmlSchemaForm.Unqualified)]
public string Title { get; set; }
[XmlElement("artist", Form = XmlSchemaForm.Unqualified)]
public Artist Artist { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[Serializable]
public class Artist
{
[XmlElement("nameWithoutThePrefix", Form = XmlSchemaForm.Unqualified)]
public string NameWithoutThePrefix { get; set; }
[XmlElement("useThePrefix", Form = XmlSchemaForm.Unqualified)]
public string UseThePrefix { get; set; }
[XmlElement("name", Form = XmlSchemaForm.Unqualified)]
public string Name { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
}

I have a class that I am serializing and deserializing. Compared to the attributes you are using... you have a lot that I don't have (on the class), but one that you are missing is [Serializable]
Mine looks like this:
[Serializable]
public class Settings
{
[XmlElement]
public int Version { get; set; }
[XmlElement]
public string Name { get; set; }
// more settings
}
So start with something simple like this, that works, then add in each new tag one at a time, and you will quickly find which one is causing it to break.
Since you are having trouble deserializing, here is the code I am using. In another class, I have:
public string FilePath { get; set; }
public Settings LoadSettings()
{
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
Settings settings = null;
using(TextReader reader = new StreamReader(this.FilePath))
{
settings = (Settings)serializer.Deserialize(reader);
}
return settings;
}

Related

Can't deserialize xml properly

I have the following string to deserialize:
<result>
<error>
<errorcode>0</errorcode>
<errorge>წარმატებით</errorge>
<errorru>Удачно</errorru>
<erroren>successfully</erroren>
<line>89</line>
</error>
<amount>
<gel>1</gel>
</amount>
<user>01001</user>
<service>MyService</service> // here
<data>
<nickname>popcorn2</nickname>
<identification_name>identified</identification_name>
<wallet_code>5554654</wallet_code>
<NationalRate>1</NationalRate>
<RATE>1</RATE>
<GENERATED_AMOUNT>1</GENERATED_AMOUNT>
<CURRENCY>GEL</CURRENCY>
</data>
<accoutant>
<agentBenefit>0</agentBenefit>
<agentCommission>0.44</agentCommission>
<clientCommission>0</clientCommission>
</accoutant>
<service>
<min_amount>0.49</min_amount>
<max_amount>1500.00</max_amount>
<currency>GEL</currency>
</service>
<avance>-134206.1500</avance>
<operation_status>0</operation_status>
</result>
as you noticed it has two tags named "service" and they have different contents
here is the class that I've been using
[XmlRoot(ElementName = "error")]
public class ErrorInfo
{
[XmlElement(ElementName = "errorcode")]
public string Errorcode { get; set; }
[XmlElement(ElementName = "errorge")]
public string Errorge { get; set; }
[XmlElement(ElementName = "errorru")]
public string Errorru { get; set; }
[XmlElement(ElementName = "erroren")]
public string Erroren { get; set; }
[XmlElement(ElementName = "line")]
public string Line { get; set; }
}
[XmlRoot(ElementName = "amount")]
public class Amount
{
[XmlElement(ElementName = "gel")]
public string Gel { get; set; }
}
[XmlRoot(ElementName = "data")]
public class Data
{
[XmlElement(ElementName = "nickname")]
public string Nickname { get; set; }
[XmlElement(ElementName = "identification_name")]
public string Identification_name { get; set; }
[XmlElement(ElementName = "wallet_code")]
public string Wallet_code { get; set; }
[XmlElement(ElementName = "NationalRate")]
public string NationalRate { get; set; }
[XmlElement(ElementName = "RATE")]
public string RATE { get; set; }
[XmlElement(ElementName = "GENERATED_AMOUNT")]
public string GENERATED_AMOUNT { get; set; }
[XmlElement(ElementName = "CURRENCY")]
public string CURRENCY { get; set; }
}
[XmlRoot(ElementName = "accoutant")]
public class Accoutant
{
[XmlElement(ElementName = "agentBenefit")]
public string AgentBenefit { get; set; }
[XmlElement(ElementName = "agentCommission")]
public string AgentCommission { get; set; }
[XmlElement(ElementName = "clientCommission")]
public string ClientCommission { get; set; }
}
[XmlRoot(ElementName = "service")]
public class Service
{
[XmlElement(ElementName = "min_amount")]
public string Min_amount { get; set; }
[XmlElement(ElementName = "max_amount")]
public string Max_amount { get; set; }
[XmlElement(ElementName = "currency")]
public string Currency { get; set; }
}
[XmlRoot(ElementName = "result")]
public class Result
{
[XmlElement(ElementName = "error")]
public ErrorInfo Error { get; set; }
[XmlElement(ElementName = "amount")]
public Amount Amount { get; set; }
[XmlElement(ElementName = "user")]
public string User { get; set; }
[XmlElement(ElementName = "data")]
public Data Data { get; set; }
[XmlElement(ElementName = "accoutant")]
public Accoutant Accoutant { get; set; }
[XmlElement(ElementName = "service")]
public Service[] Service { get; set; }
[XmlElement(ElementName = "avance")]
public string Avance { get; set; }
[XmlElement(ElementName = "operation_status")]
public string Operation_status { get; set; }
}
I tried to handle service tags as array but it could not work. I was able to get values from the last tag , but not from the first one. both tags are neccessary so i am wondering if there is a way to get values from both tags ?
Visual Studio has this really great feature where you can put your XML in the clipboard and then do Edit -> Paste special -> Paste XML as classes.
If you do that with your XML, it generates the following class for the service tag:
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class resultService
{
private decimal min_amountField;
private decimal max_amountField;
private string currencyField;
private string[] textField;
/// <remarks/>
public decimal min_amount
{
get
{
return this.min_amountField;
}
set
{
this.min_amountField = value;
}
}
/// <remarks/>
public decimal max_amount
{
get
{
return this.max_amountField;
}
set
{
this.max_amountField = value;
}
}
/// <remarks/>
public string currency
{
get
{
return this.currencyField;
}
set
{
this.currencyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text
{
get
{
return this.textField;
}
set
{
this.textField = value;
}
}
}
If you want to do it manually, the secret seems to be to add a field with the XmlTextAttribute attribute.
You can not have two different elements with the same name. It won't know which tag belongs where, you have to alter the name to "MyServiceName" or something similar.
Change your classes as follows:
[XmlRoot(ElementName = "result")]
public class Result
{
// no need XmlElement attribute
public ServiceComplex ServiceComplex { get; set; }
// no need XmlElement attribute
public string ServiceSimple { get; set; }
// other properties
}
// no need XmlRoot attribute
public class ServiceComplex
{
[XmlElement(ElementName = "min_amount")]
public string Min_amount { get; set; }
[XmlElement(ElementName = "max_amount")]
public string Max_amount { get; set; }
[XmlElement(ElementName = "currency")]
public string Currency { get; set; }
}
Instead of ServiceComplex and ServiceSimple, choose the names that suit you.
Subscribe XmlSerializer to UnknownElement event:
var xs = new XmlSerializer(typeof(Result));
xs.UnknownElement += Xs_UnknownElement;
private static void Xs_UnknownElement(object sender, XmlElementEventArgs e)
{
if (e.Element.Name == "service")
{
var result = (Result)e.ObjectBeingDeserialized;
if (e.Element.ChildNodes.Count == 1)
{
result.ServiceSimple = e.Element.InnerText;
}
else
{
result.ServiceComplex = new ServiceComplex
{
Min_amount = e.Element.SelectSingleNode("min_amount").InnerText,
Max_amount = e.Element.SelectSingleNode("max_amount").InnerText,
Currency = e.Element.SelectSingleNode("currency").InnerText
};
}
}
}
In the event handler, we manually populate the properties of our class from xml.

How to deserialize the following XML response in C#

I received the following response from a java web service. When try to deserialize it using DataContractSerializer, I received the following error
<ns:redeemVoucherResponse xmlns:ns="http://vouchers.example.com">
<ns:return xmlns:ax236="http://util.vouchers.example.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax236:RedeemStatus">
<ax236:status>fail</ax236:status>
<ax236:statusCode>4</ax236:statusCode>
<ax236:statusMessage>Error message</ax236:statusMessage>
<ax236:redeemData xsi:nil="true" />
</ns:return>
</ns:redeemVoucherResponse>
Error in line 1 position 65. Expecting element VoucherService.redeemVoucherResponse' from namespace 'http://schemas.datacontract.org/2004/07/Service.Internal.VoucherServices'.. Encountered 'Element' with name 'redeemVoucherResponse', namespace 'http://vouchers.example.com'.
Classes use for the deserialization:
public class redeemVoucherResponse {
[DataMember]
public RedeemStatus #return { get; set; }
}
public class RedeemStatus {
[DataMember]
public string status { get; set; }
[DataMember]
public byte statusCode { get; set; }
[DataMember]
public string statusMessage { get; set; }
[DataMember]
public object redeemData { get; set; }
}
Code use for the deserialization:
HttpWebResponse objWebResponse = HttpWebResponse)objWebRequest.GetResponse();
Stream objResponseStream = objWebResponse.GetResponseStream();
Type objType = typeof(redeemVoucherResponse);
DataContractSerializer objXmlSerializer = new DataContractSerializer(objType);
redeemVoucherResponse objMessage = (redeemVoucherResponse)objXmlSerializer.ReadObject(objResponseStream);
Try this ... not tested though.
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://vouchers.example.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://vouchers.example.com", IsNullable = false)]
public class redeemVoucherResponse
{
public redeemVoucherResponseReturn #return { get; set; }
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://vouchers.example.com")]
public class redeemVoucherResponseReturn
{
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://util.vouchers.example.com/xsd")]
public string status { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://util.vouchers.example.com/xsd")]
public byte statusCode { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://util.vouchers.example.com/xsd")]
public string statusMessage { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://util.vouchers.example.com/xsd", IsNullable = true)]
public object redeemData { get; set; }
}

Entity Framework 5 DbUpdateException: Null value for non-nullable member

I have some data models:
[DataContract(Name = "artist")]
public class artist : IEqualityComparer<artist>
{
[Key]
[XmlIgnore]
[DataMember]
public int ID { get; set; }
[DataMember]
[XmlElement(ElementName = "name")]
public string name { get; set; }
[DataMember]
[XmlElement(ElementName = "mbid", IsNullable = true)]
public string mbid { get; set; }
[DataMember]
[XmlElement(ElementName = "url")]
public string url { get; set; }
[XmlElement(ElementName = "image", IsNullable = true)]
public List<string> image { get; set; }
[DataMember(IsRequired=false)]
[XmlElement(ElementName = "stats", IsNullable = true)]
public stats stats { get; set; }
public double? match { get; set; }
public List<tag> tags { get; set; }
[XmlElement(ElementName = "similar")]
[DataMember(Name = "similar")]
public List<artist> similar { get; set; }
[DataMember]
[XmlElement(ElementName = "bio", IsNullable = true)]
public wiki bio { get; set; }
public bool Equals(artist x, artist y)
{
return x.name == y.name;
}
public int GetHashCode(artist obj)
{
return obj.name.GetHashCode();
}
}
and a complex type:
[DataContract]
[ComplexType]
[XmlRoot(ElementName = "streamable", IsNullable = true)]
public class stats
{
[DataMember(IsRequired = false)]
public int listeners { get; set; }
[DataMember(IsRequired = false)]
public int playcount { get; set; }
}
and database inclusion:
[Table("CachedArtistInfo")]
public class MusicArtists
{
[Key]
public string artistName { get; set; }
public artist artistInfo { get; set; }
private DateTime _added = default(DateTime);
[DataMember(IsRequired = true)]
[Timestamp]
public DateTime added
{
get
{
return (_added == default(DateTime)) ? DateTime.Now : _added;
}
set { _added = value; }
}
}
Final step:
foreach (artist a in id)
{
df.CachedArtists.Add(new MusicArtists() { artistName = a.name, artistInfo = a });
df.SaveChanges();
}
ERROR:
ExceptionType
"System.Data.Entity.Infrastructure.DbUpdateException"
"Null value for non-nullable member. Member: 'stats'."
a variable is fully filled and object stats in it.
what's wrong?
Complex types cannot be null - so you have to create an instance of the class before saving. This article helped me solve the same problem.
http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx

deserialize xml in complex object

I can't understand why object is null:
WebClient browse = new WebClient();
StreamReader res = new StreamReader(browse.OpenRead("http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&track=believe"));
string result = res.ReadToEnd();
XmlDocument xmltrackinfo = new XmlDocument();
xmltrackinfo.InnerXml = result;
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "lfm";
xRoot.IsNullable = true;
XmlSerializer xs = new XmlSerializer(typeof(fm), xRoot);
fm rez = (fm) xs.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(result)));
Object Model:
[Serializable()]
[XmlRoot(ElementName = "lfm", IsNullable = true)]
public class fm
{
[XmlElement("lfm")]
public Track lfm { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "artist", IsNullable = true)]
public class artist
{
public string name { get; set; }
public string mbid { get; set; }
public string url { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "album", IsNullable = true)]
public class album
{
public string artist { get; set; }
public string title { get; set; }
public string mbid { get; set; }
public string url { get; set; }
public List<string> image { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "tag", IsNullable = true)]
public class tag
{
public string name { get; set; }
public string url { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "wiki", IsNullable = true)]
public class wiki
{
public string summary { get; set; }
public string content { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "track", IsNullable = true)]
public class Track
{
public string id { get; set; }
public string name { get; set; }
public string mbid { get; set; }
public string url { get; set; }
public string duration { get; set; }
public string streamable { get; set; }
public string listeners { get; set; }
public string playcount { get; set; }
public artist artist { get; set; }
public album album { get; set; }
public List<tag> toptags { get; set; }
public wiki wiki { get; set; }
}
and XML:
http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&track=believe
so what should I do?
Try renaming your fm class to lfm.
public class lfm
{
public Track track { get; set; }
}
and then you could also get rid of the xRoot variable:
XmlSerializer xs = new XmlSerializer(typeof(lfm));
lfm rez = (lfm) xs.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(result)));
Also you don't need the [Serializable] attribute. This is used for binary serialization and is completely ignored by the XmlSerializer class.
The lfm property of the fm class must have track as its XmlElement:
[Serializable()]
[XmlRoot(ElementName = "lfm", IsNullable = true)]
public class fm
{
[XmlElement("track")]
public Track lfm { get; set; }
}

asp.net MVC cannot view properties from class used to de-serialize XML

I am hoping to DE-serialize some XML, so I have used XSD to convert the xml into a class.
using System.Xml.Serialization;
namespace Testing.Models
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public class TradeDoublerProducts
{
private TradeDoublersProductsProduct[] itemsField;
[XmlElementAttribute("product", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public TradeDoublersProductsProduct[] Items { get; set; }
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
public class TradeDoublersProductsProduct
{
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string name { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string productUrl { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string imageUrl { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string description { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string price { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TDProductId { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TDCategoryID { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string sku { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string shortDescription { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string promoText { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string previousPrice { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string shippingCost { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string weight { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string size { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string brand { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string model { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string condition { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string mpn { get; set; }
[XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string techSpecs { get; set; }
}
}
this all build fine, and looks fine, however I don't think I am doing things right here:
public class ProcessTradeDoubler
{
public void ProcessMyFeed(int feedId)
{
//TODO read XML data in from specified url matching id sent
ProcessXml("data from url");
}
private static void ProcessXml(string myXml)
{
var ser = new XmlSerializer(typeof(TradeDoublerProducts));
TradeDoublerProducts tradeDoublerProducts;
using (XmlReader reader = XmlReader.Create(myXml))
{
tradeDoublerProducts = (TradeDoublerProducts)ser.Deserialize(reader);
}
AddModelToProducts((IEnumerable<TradeDoublerProducts>) tradeDoublerProducts);
}
private static void AddModelToProducts(IEnumerable<TradeDoublerProducts> model)
{
// loop through model and add items to database
foreach (var p in model)
{
// this does not work, there is no properties inside Items p.Items.name;
}
}
}
I would expect properties inside my foreach on the model for model.items
but I have nothing, please note this is not to test if it reads the XML, or anything to do with the XML file, this is just the pure code and the fact that I should be able to access properties of said XML once i have put into testing.
thanks
Items is an array of items. Foreach over them, and you should find the properties of each item.
the fix was on my execution end:
should read:
var ser = new XmlSerializer(typeof(TradeDoublerProducts));
TradeDoublerProducts tradeDoublerProducts;
using (XmlReader reader = XmlReader.Create(myXml))
{
tradeDoublerProducts = (TradeDoublerProducts) ser.Deserialize(reader);
}
IEnumerable<TradeDoublersProductsProduct> model = tradeDoublerProducts.Items;
AddModelToProducts(model);
as marc pointed out i was trying to take an enumerable tradeDoublerProducts, when in actual fact the tradeDoublerProducts.items was the enumerable item i should have been passing over. the code above works great.

Categories