Got XML that i want to deserialize to Object.
There is no questions if i deserialize only non-multiple value element, but i got no idea what to do with an array of value elements in value element.
There is always UnknownNode with LocalName = #text,
XML to deserialize
<?xml version="1.0" encoding="UTF-8"?>
<issue>
<custom_fields type="array">
<custom_field id="11" name="Version">
<value>7.9.18.31</value>
</custom_field>
<custom_field id="89" name="Tags" multiple="true">
<value type="array">
<value>Tag1</value>
<value>Tag3</value>
<value>Tag6</value>
</value>
</custom_field>
<custom_field id="90" name="started_on">
<value>2017-08-25</value>
</custom_field>
</custom_fields>
</issue>
class i get with xsd.exe
using System.Xml.Serialization;
[XmlRoot(IsNullable=false)]
public partial class issue
{
private object[] itemsField;
[XmlElement("custom_fields", typeof(issueCustom_fields))]
[XmlElement("value", typeof(Value))]
public object[] Items
{
get { return itemsField; }
set { itemsField = value; }
}
}
public class Value
{
private Value[] valueField;
private string typeField;
[XmlElement("value")]
public Value[] value
{
get { return valueField; }
set { valueField = value; }
}
[XmlAttribute]
public string type
{
get { return typeField; }
set { typeField = value; }
}
}
public class issueCustom_fields
{
private issueCustom_fieldsCustom_field[] custom_fieldField;
private string typeField;
[XmlElement("custom_field")]
public issueCustom_fieldsCustom_field[] custom_field
{
get { return custom_fieldField; }
set { custom_fieldField = value; }
}
[XmlAttribute]
public string type
{
get { return typeField; }
set { typeField = value; }
}
}
public class issueCustom_fieldsCustom_field
{
private Value[] valueField;
private string idField;
private string nameField;
private string multipleField;
[XmlElement("value")]
public Value[] value
{
get { return valueField; }
set { valueField = value; }
}
[XmlAttribute]
public string id
{
get { return idField; }
set { idField = value; }
}
[XmlAttribute]
public string name
{
get { return nameField; }
set { nameField = value; }
}
[XmlAttribute]
public string multiple
{
get { return multipleField; }
set { multipleField = value; }
}
}
Deserializing code
var ser = new XmlSerializer(typeof(issue));
var issue = new issue();
using (var sr = new StreamReader("testIssues.xml"))
issue = (issue)ser.Deserialize(sr);
i can get data using:
ser.UnknownNode += (s, ea) =>
{
if (ea.LocalName == "#text" && ea.ObjectBeingDeserialized is Value)
{
Value val = (Value)ea.ObjectBeingDeserialized;
if (val.value == null)
{
val.Val = ea.Text;
}
}
};
but is there a way to get it more smoothe way using just deserialization?
Have you tried to use XmlArrayAttribute?
[XmlArray("value")]
public Value[] Items
{
get { return itemsField; }
set { itemsField = value; }
}
[XmlType("Value")]
public class Value
{
....
}
Check out this question.
Related
new to ASP.net and have a question involving a model that has multiple variables of the same type.
I have Order model that can have 0 to n Detail models attached to it. When I POST the http request I get all the information for the Order except the Detail model always comes back as null. Or more specifically the ".count" method on the List is 0.
How can I get it to post all the Details for my Model?
Order Model:
public struct shipTo
{
public string id;
public string Line1;
public int PostalCode;
public string City;
public string State;
public string CountryCode;
}
namespace USS_EDIv2.Models
{
public class Order
{
public Int64 SalesOrderNumber { get; set; }
public Int64 PurchaseOrderNumber { get; set; }
public Int64 BranchPlant { get; set; }
public shipTo ShipTo;
public Int64 Quantity { get; set; }
public string UOM { get; set; }
public List<Detail> Detail = new List<Detail>();
}
}
Detail Model:
namespace USS_EDIv2.Models
{
public class Detail
{
public string LineNumber;
public string GradeItem;
public string Quantity;
public string UOM;
public string RequestDate;
public string Status;
public Detail()
{
LineNumber = "1";
GradeItem = "1";
Quantity = "1";
UOM = "1";
RequestDate = "1";
Status = "1";
}
}
}
Repository class:
namespace USS_EDIv2.Services
{
public class SalesRepository
{
public DateTime currentTime = System.DateTime.Now;
private const string CacheKey = "4041tmtTEST1337";
public string xml;
public int i;
public Order[] GetAllSales()
{
var ctx = HttpContext.Current;
if (ctx != null)
{
return (Order[])ctx.Cache[CacheKey];
}
return new Order[]
{
new Order
{
}
};
}
public SalesRepository()
{
var ctx = HttpContext.Current;
if (ctx != null)
{
if (ctx.Cache[CacheKey] == null)
{
var sales = new Order[]
{
};
{
};
ctx.Cache[CacheKey] = sales;
//ctx.Cache.Remove("4041tmtTEST1337");
}
}
}
public bool SaveSale(Order sales)
{
var ctx = HttpContext.Current;
if (ctx != null)
{
try
{
var currentData = ((Order[])ctx.Cache[CacheKey]).ToList();
currentData.Add(sales);
ctx.Cache[CacheKey] = currentData.ToArray();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
return false;
}
}
}
And my Controller:
namespace USS_EDIv2.Controllers
{
public class NewOrderController : ApiController
{
public DateTime currentTime = System.DateTime.Now;
private SalesRepository salesRepository;
public int i;
public NewOrderController()
{
this.salesRepository = new SalesRepository();
}
public Order[] Get()
{
return salesRepository.GetAllSales();
}
public HttpResponseMessage Post(Order sale)
{
var ctx = HttpContext.Current;
this.salesRepository.SaveSale(sale);
var response = Request.CreateResponse<Order> (System.Net.HttpStatusCode.Created, sale);
return response;
}
}
}
This is the XML data being requested:
<Order>
<SalesOrderNumber>1294288</SalesOrderNumber>
<PurchaseOrderNumber>81896</PurchaseOrderNumber>
<BranchPlant>9701</BranchPlant>
<ShipTo id="string">
<Line1>RAIL TRACK #769 SPOT 00</Line1>
<PostalCode>79765</PostalCode>
<City>MIDLAND COUNTY</City>
<State>TX</State>
<CountryCode>US</CountryCode>
</ShipTo>
<Quantity>75</Quantity>
<UOM>TN</UOM>
<Detail created="2015-12-14T13:59:57.84" action="Create">
<LineNumber>1.0</LineNumber>
<GradeItem>97010B00000</GradeItem>
<Quantity>25.000</Quantity>
<UOM>TN</UOM>
<RequestDate>2015-07-11</RequestDate>
<Status>Open</Status>
</Detail>
<Detail created="2015-12-14T13:59:57.84" action="Create">
<LineNumber>2.0</LineNumber>
<GradeItem>97010B00000</GradeItem>
<Quantity>25</Quantity>
<UOM>TN</UOM>
<RequestDate>2002-02-07</RequestDate>
<Status>Open</Status>
</Detail>
<Detail created="2015-05-22T02:29:50.78" action="Create">
<LineNumber>3.0</LineNumber>
<GradeItem>97010B00000</GradeItem>
<Quantity>25</Quantity>
<UOM>TN</UOM>
<RequestDate>2015-07-11</RequestDate>
<Status>Open</Status>
</Detail>
</Order>
And this is my current response:
<ArrayOfOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Order>
<ShipTo>
<Line1>RAIL TRACK #769 SPOT 00</Line1>
<PostalCode>79765</PostalCode>
<City>MIDLAND COUNTY</City>
<State>TX</State>
<CountryCode>US</CountryCode>
</ShipTo>
************HERE LIES THE PROBLEM
<Detail/>
************
<SalesOrderNumber>1294288</SalesOrderNumber>
<PurchaseOrderNumber>81896</PurchaseOrderNumber>
<BranchPlant>9701</BranchPlant>
<Quantity>75</Quantity>
<UOM>TN</UOM>
</Order>
</ArrayOfOrder>
I also have no control over changing the format of the incoming xml data. Any pointer in the right direction would be helpful please.
Debug:
[debug] (http://imgur.com/RVnpZ0S)
Okay,
So I solved this by copying the XML data onto my clipboard, creating a new class and then going to Edit -> Paste Special -> Paste XML as classes
and it came up with this:
namespace USS_EDIv2.Models
{
public class Orders
{
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Order
{
private uint salesOrderNumberField;
private uint purchaseOrderNumberField;
private ushort branchPlantField;
private OrderShipTo shipToField;
private byte quantityField;
private string uOMField;
private OrderDetail[] detailField;
/// <remarks/>
public uint SalesOrderNumber
{
get
{
return this.salesOrderNumberField;
}
set
{
this.salesOrderNumberField = value;
}
}
/// <remarks/>
public uint PurchaseOrderNumber
{
get
{
return this.purchaseOrderNumberField;
}
set
{
this.purchaseOrderNumberField = value;
}
}
/// <remarks/>
public ushort BranchPlant
{
get
{
return this.branchPlantField;
}
set
{
this.branchPlantField = value;
}
}
/// <remarks/>
public OrderShipTo ShipTo
{
get
{
return this.shipToField;
}
set
{
this.shipToField = value;
}
}
/// <remarks/>
public byte Quantity
{
get
{
return this.quantityField;
}
set
{
this.quantityField = value;
}
}
/// <remarks/>
public string UOM
{
get
{
return this.uOMField;
}
set
{
this.uOMField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Detail")]
public OrderDetail[] Detail
{
get
{
return this.detailField;
}
set
{
this.detailField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderShipTo
{
private string line1Field;
private uint postalCodeField;
private string cityField;
private string stateField;
private string countryCodeField;
private string idField;
/// <remarks/>
public string Line1
{
get
{
return this.line1Field;
}
set
{
this.line1Field = value;
}
}
/// <remarks/>
public uint PostalCode
{
get
{
return this.postalCodeField;
}
set
{
this.postalCodeField = value;
}
}
/// <remarks/>
public string City
{
get
{
return this.cityField;
}
set
{
this.cityField = value;
}
}
/// <remarks/>
public string State
{
get
{
return this.stateField;
}
set
{
this.stateField = value;
}
}
/// <remarks/>
public string CountryCode
{
get
{
return this.countryCodeField;
}
set
{
this.countryCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetail
{
private decimal lineNumberField;
private string gradeItemField;
private decimal quantityField;
private string uOMField;
private System.DateTime requestDateField;
private string statusField;
private System.DateTime createdField;
private string actionField;
/// <remarks/>
public decimal LineNumber
{
get
{
return this.lineNumberField;
}
set
{
this.lineNumberField = value;
}
}
/// <remarks/>
public string GradeItem
{
get
{
return this.gradeItemField;
}
set
{
this.gradeItemField = value;
}
}
/// <remarks/>
public decimal Quantity
{
get
{
return this.quantityField;
}
set
{
this.quantityField = value;
}
}
/// <remarks/>
public string UOM
{
get
{
return this.uOMField;
}
set
{
this.uOMField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime RequestDate
{
get
{
return this.requestDateField;
}
set
{
this.requestDateField = value;
}
}
/// <remarks/>
public string Status
{
get
{
return this.statusField;
}
set
{
this.statusField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime created
{
get
{
return this.createdField;
}
set
{
this.createdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string action
{
get
{
return this.actionField;
}
set
{
this.actionField = value;
}
}
}
Just in case anyone ever stumbles across this problem. What was giving me hours of headache took 3 clicks to solve... Thanks all for your replies.
I'm using DataContractJsonSerializer to deserialize JSON from a Webservice.
[{"id":2947,"type":"PdfDocument","name":"test.pdf"},
{"id":2945,"type":"ImageDocument","name":"test.jpg", "color": "green"}]
Based on their type, JSON entities can have different properties (for example a color). Currently i use a single model Document for deserialization and PagedCollectionViews to filter what i need to show in the UI.
public class Document : EntityBase
{
private string name;
private string type;
private string color;
public Document()
{
}
[DataMember(Name = "name")]
public string Name
{
get
{
return this.name;
}
set
{
if (this.name != value)
{
this.name = value;
this.OnPropertyChanged("Name");
}
}
}
[DataMember(Name = "type")]
public string Type
{
get
{
return this.type;
}
set
{
if (this.type != value)
{
this.type = value;
this.OnPropertyChanged("Type");
}
}
}
[DataMember(Name = "color")]
public string Color
{
get
{
return this.color;
}
set
{
if (this.color != value)
{
this.color= value;
this.OnPropertyChanged("Color");
}
}
}
}
public class DocumentsViewModel : ViewModelBase
{
public ObservableCollection<Document> Documents { get; set; }
public PagedCollectionView ImageDocuments;
public PrintProjectDetailsViewModel()
{
this.Documents = new ObservableCollection<Document>();
this.ImageDocuments = new PagedCollectionView(Documents);
this.ImageDocuments.Filter = (o) => ((Document)o).Type == "ImageDocument";
}
}
However, that feels "smelly" to me. Is it possible to automatically deserialize to a subclass of Document depending of the JSON value of type?
I have a customer object class:
public class customerObject
{
private string _address1;
private string _address2;
private string _address3;
private string _category;
private string _country;
private string _county;
private string _custcode;
private string _fullname;
private string _int_rep_hou;
private string _int_rep_key;
private double _lat;
private double _lng;
private string _postcode;
private string _rep_code;
private string _telephone;
public customerObject()
{
}
public string Address1
{
get { return _address1; }
set { _address1 = value; }
}
public string Address2
{
get
{
return _address2;
}
set { _address2 = value; }
}
public string Address3 { get { return _address3; } set { _address3 = value; } }
public string Category
{
get { return _category; }
set { _category = value; }
}
public string Country { get { return _country; } set { _country = value; } }
public string County { get { return _county; } set { _county = value; } }
public string Custcode
{
get { return _custcode; }
set { _custcode = value; }
}
public string Fullname
{
get { return _fullname; }
set { _fullname = value; }
}
public string Int_rep_hou
{
get { return _int_rep_hou; }
set { _int_rep_hou = value; }
}
public string Int_rep_key
{
get { return _int_rep_key; }
set { _int_rep_key = value; }
}
public double Lat { get { return _lat; } set { _lat = value; } }
public double Lng { get { return _lng; } set { _lng = value; } }
public string Postcode { get { return _postcode; } set { _postcode = value; } }
public string Rep_code
{
get { return _rep_code; }
set { Rep_code = value; }
}
public string Telephone { get { return _telephone; } set { _telephone = value; }
}
}
I have a CustomCollections class
public class CustomerCollection
{
public List<customerObject> Customers { get; set; }
}
My method that loops through dt rows and converts to a customer object
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//Create Collection Object
Valueobjects.CustomerCollection Collection = new Valueobjects.CustomerCollection();
foreach (DataRow row in temptablename.Rows)
{
//Create Customer Object
Valueobjects.customerObject Customer = new Valueobjects.customerObject();
//set values of customer object
Customer.Rep_code = "";
Customer.Int_rep_key = "";
Customer.Int_rep_hou = "";
Customer.Fullname = row["Fullname"].ToString().Trim();
Customer.Custcode = row["Custcode"].ToString().Trim();
Customer.Category = row["Category"].ToString().Trim();
Customer.Address1 = row["Address1"].ToString().Trim();
Customer.Address2 = row["Address2"].ToString().Trim();
Customer.Address3 = row["Address3"].ToString().Trim();
Customer.Postcode = row["Postcode"].ToString().Trim();
Customer.Country = row["Country"].ToString().Trim();
Customer.Telephone = row["Telephone"].ToString().Trim();
Customer.Lat = Convert.ToDouble(row["Lat"]);
Customer.Lng = Convert.ToDouble(row["Lng"]);
Customer.County = row["County"].ToString().Trim();
//add to the collection (list)
Collection.Customers.Add(Customer);
}
temptablename = null;
return Collection;
}
However when I create a new Customer object and a new CustomerCollection object I am getting an error when adding the customer to the collection list.
Error:
Error 32 Cannot implicitly convert type
'Classes.Valueobjects.CustomerCollection' to
'System.Collections.Generic.List'
Your method is returning a List<CustomerCollection>:
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//...
}
But the code is trying to return a CustomerCollection:
return Collection;
Just as the error says, these two types are different.
If a CustomerCollection is already a collection of customers, then semantically what is a List<Valueobjects.CustomerCollection>? A collection of collections? It seems like you're over-pluralizing your objects :)
There are two approaches here. Either return a CustomerCollection from the method:
public CustomerCollection dolist(DataTable temptablename)
{
//...
}
Or use a List<Customer> if you want to use generic lists as your collection containers:
public List<Customer> dolist(DataTable temptablename)
{
//...
var Collection = new List<Customer>();
//...
Collection.Add(Customer);
//...
return Collection;
}
Side note: You may want to stick to C# conventions for variable naming. As you can see from the code highlighting here on Stack Overflow, your variable names can easily be mistaken for classes/types, which can cause confusion when supporting the code.
Return a CustomerCollection instead of a List<Valueobjects.CustomerCollection>:
public Valueobjects.CustomerCollection Dolist(DataTable temptablename)
{
// ...
Your object has a list, it is not a list.
MSDN: Inheritance
I want to get an attribute from vimeo xml.. here is structure of xml document
<?xml version="1.0" encoding="UTF-8" ?>
- <rsp generated_in="0.6533" stat="ok">
- <videos on_this_page="15" page="1" perpage="15" total="329">
- <video allow_adds="1" embed_privacy="anywhere" id="3475223" is_hd="0" is_transcoding="0" license="0" privacy="anybody">
<title>AxDroid - Android on Dell Axim x51v</title>
<description>This is my first attempt at installing and running Android on my Dell Axim x51v. Touchscreen and buttons are working! For details please visit: http://axdroid.blogspot.com/</description>
<upload_date>2009-03-04 16:14:19</upload_date>
<modified_date>2012-07-14 07:03:32</modified_date>
<number_of_likes>2</number_of_likes>
<number_of_plays>43422</number_of_plays>
<number_of_comments>1</number_of_comments>
<width>320</width>
<height>240</height>
<duration>320</duration>
- <owner display_name="Ertan D." id="1387509" is_plus="0" is_pro="0" is_staff="0" profileurl="http://vimeo.com/user1387509" realname="Ertan D." username="user1387509" videosurl="http://vimeo.com/user1387509/videos">
- <portraits>
<portrait height="30" width="30">http://a.vimeocdn.com/images_v6/portraits/portrait_30_yellow.png</portrait>
<portrait height="75" width="75">http://a.vimeocdn.com/images_v6/portraits/portrait_75_yellow.png</portrait>
<portrait height="100" width="100">http://a.vimeocdn.com/images_v6/portraits/portrait_100_yellow.png</portrait>
<portrait height="300" width="300">http://a.vimeocdn.com/images_v6/portraits/portrait_300_yellow.png</portrait>
</portraits>
</owner>
- <tags>
<tag author="1387509" id="8397224" normalized="android" url="http://vimeo.com/tag:android">android</tag>
<tag author="1387509" id="8397225" normalized="dell" url="http://vimeo.com/tag:dell">dell</tag>
<tag author="1387509" id="8397226" normalized="axim" url="http://vimeo.com/tag:axim">axim</tag>
<tag author="1387509" id="8397227" normalized="linux" url="http://vimeo.com/tag:linux">linux</tag>
<tag author="1387509" id="8397228" normalized="google" url="http://vimeo.com/tag:google">google</tag>
<tag author="1387509" id="8397229" normalized="pda" url="http://vimeo.com/tag:pda">pda</tag>
<tag author="1387509" id="8397230" normalized="ppc" url="http://vimeo.com/tag:ppc">ppc</tag>
</tags>
- <cast>
<member display_name="Ertan D." id="1387509" role="" username="user1387509" />
</cast>
- <urls>
<url type="video">http://vimeo.com/3475223</url>
</urls>
- <thumbnails>
<thumbnail height="75" width="100">http://b.vimeocdn.com/ts/347/807/3478071_100.jpg</thumbnail>
<thumbnail height="150" width="200">http://b.vimeocdn.com/ts/347/807/3478071_200.jpg</thumbnail>
<thumbnail height="480" width="640">http://b.vimeocdn.com/ts/347/807/3478071_640.jpg</thumbnail>
</thumbnails>
</video>
- <video allow_adds="1" embed_privacy="anywhere" id="28665952" is_hd="1" is_transcoding="0" license="0" privacy="anybody">
<title>Duygu + Ertan Şıkır Şıkır by DÜĞÜNHİKAYEMİZ</title>
<description />
<upload_date>2011-09-06 10:54:49</upload_date>
<modified_date>2012-07-14 06:41:33</modified_date>
<number_of_likes>3</number_of_likes>
<number_of_plays>26214</number_of_plays>
and I want to get username attribute from owner element. Here is serialization code.
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "rsp")]
public partial class VimeoSearchResponse
{
private SearchResponseVideosWrapper _videos;
private string _generated_in;
private string _stat;
[XmlElementAttribute("videos", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public SearchResponseVideosWrapper videos
{
get { return _videos; }
set { _videos = value; }
}
[XmlAttributeAttribute()]
public string generated_in
{
get { return _generated_in; }
set { _generated_in = value; }
}
[XmlAttributeAttribute()]
public string stat
{
get { return _stat; }
set { _stat = value; }
}
}
[SerializableAttribute]
[XmlTypeAttribute(AnonymousType = true)]
public partial class SearchResponseVideosWrapper
{
private SearchResponseVideosWrapperVideo[] _video;
private string _on_this_page;
private string _page;
private string _perpage;
private string _total;
[XmlElementAttribute("video", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public SearchResponseVideosWrapperVideo[] video
{
get { return _video; }
set { _video = value; }
}
[XmlAttributeAttribute()]
public string on_this_page
{
get { return _on_this_page; }
set { _on_this_page = value; }
}
[XmlAttributeAttribute()]
public string page
{
get { return _page; }
set { _page = value; }
}
[XmlAttributeAttribute()]
public string perpage
{
get { return _perpage; }
set { _perpage = value; }
}
[XmlAttributeAttribute()]
public string total
{
get { return _total; }
set { _total = value; }
}
}
[SerializableAttribute]
[XmlTypeAttribute(AnonymousType = true)]
public partial class SearchResponseVideosWrapperVideo
{
private string _title;
private string _id;
private string _username;
[XmlElement()]
public string title
{
get { return _title; }
set { _title = value; }
}
[XmlAttributeAttribute()]
public string id
{
get { return _id; }
set { _id = value; }
}
[XmlElementAttribute("owner")]
public string username
{
get { return _username; }
set { _username = value; }
}
}
problem is here i think
[XmlElementAttribute("owner")]
public string username
{
get { return _username; }
set { _username = value; }
}
- <owner display_name="Ertan D." id="1387509" is_plus="0" is_pro="0" is_staff="0" profileurl="http://vimeo.com/user1387509" realname="Ertan D." username="user1387509" videosurl="http://vimeo.com/user1387509/videos">
how can I get attribute from owner..
here is exception detail
I get an exception that is There is an error in XML document (1,
1005).
{"Unexpected node type Element. ReadElementString method can only be
called on elements with simple or empty content. Line 1, position
1005."}
System.InvalidOperationException was caught
You need an 'owner' class. You also might consider adding the 'portrait' class with a collection in the 'owner' class.
public class owner
{
[XmlAttributeAttribute]
public string username { get; set; }
}
[SerializableAttribute]
[XmlTypeAttribute(AnonymousType = true)]
public partial class SearchResponseVideosWrapperVideo
{
private string _title;
private string _id;
private string _username;
[XmlElement()]
public string title
{
get { return _title; }
set { _title = value; }
}
[XmlAttributeAttribute()]
public string id
{
get { return _id; }
set { _id = value; }
}
[XmlElementAttribute("owner")]
public owner owner { get; set; }
}
You are annotating username with an XmlElementAttribute for "owner."
That implies that the owner element should be deserialized into the string property username.
If you want to get at username, you first have to deserialize owner into some object.
For example, you could add an Owner class, in the same manner as you created VimeoSearchResponse.
public class Owner
{
private string _owner;
[XmlAttribute]
public string username
{
get { return _owner; }
set { _owner = value; }
}
}
Here is the thing, I have a problem creating a new object using the remote mechanism "marshal by value".
Here is my class:
[Serializable]
internal class Empleado_MBV
{
public Empleado_MBV()
{
Id = 123456789;
Nombres = "NotEntry";
Apellidos = "NotEntry";
FechaNacimiento = DateTime.MinValue;
Direccion = "NotEntry";
Metapreferencias = "NotEntry";
}
private List<Multas> _multas;
internal List<Multas> Multas
{
get { return _multas; }
set { _multas = value; }
}
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _nombres;
public string Nombres
{
get { return _nombres; }
set { _nombres = value; }
}
private string _apellidos;
public string Apellidos
{
get { return _apellidos; }
set { _apellidos = value; }
}
private DateTime _FecNac;
public DateTime FechaNacimiento
{
get { return _FecNac; }
set { _FecNac = value; }
}
private string _direccion;
public string Direccion
{
get { return _direccion; }
set { _direccion = value; }
}
private string _metapreferencias;
public string Metapreferencias
{
get { return _metapreferencias; }
set { _metapreferencias = value; }
}
public string _AppDomainHost
{
get { return AppDomain.CurrentDomain.FriendlyName.ToString(); }
}
}
But when I try to create an object in another "appdomain", the property "_AppDomainHost" of "Empleado" does not show the "appdomain" I had created, but show the "appdomain" by default. Some ideas?
AppDomain ad1 = AppDomain.CreateDomain("NewAppDomain");
//Crear new object in my new AD.
Empleado_MBV mbv_emp = (Empleado_MBV)ad1.CreateInstanceFromAndUnwrap("DEMO_MBV_MBR.exe", "DEMO_MBV_MBR.Empleado_MBV");
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName.ToString());
Console.WriteLine("MBV : {0}",mbv_emp._AppDomainHost.ToString());
Console.ReadLine();
Result:
DEMO_MBV_MBR.vshost.exe
MBV : DEMO_MBV_MBR.vshost.exe
The result that I want:
DEMO_MBV_MBR.vshost.exe
MBV : NewAppDomain
You need to store AppDomain in Empleado_MBV's constructor.
What you are doing right now is displaying current AppDomain using its Current static property. It will return the AppDomain where current code is being executed.
Example:
private string _appDomainHost;
public string _AppDomainHost
{
get { return _appDomainHost; }
}
and in constructor:
_appDomainHost = AppDomain.CurrentDomain.FriendlyName.ToString();