I have the following XML document
- "1.0" encoding = "utf-8"?>
"0001" createdon ="11/02/20" expireson="12/02/20">"A10" >1977 Skyhawk Light blue and white - New paint, nearly new interior,
- 685 hours SMOH, full IFR King avionics
23,495 "s001" phone="123-123-123"> Skyway Aircraft"s002" phone="123-123-222"> Boeing"s003" phone="123-123-233"> McDouglas"1000" from="12/03/16" to="12/03/18" no="M0001">Silver"1000" from="12/03/16" to="12/03/18" no="M0002">Gold"1000" from="12/03/16" to="12/03/18" no="M0003">PlatinumRapid City, South Dakota "002" createdon ="11/05/20" expireson="12/05/20">1965 &p; Cherokee Gold - 240 hours SMOH, dual NAVCOMs, DME,
- new Cleveland brakes, great shape
"555-333-2222" email="[email protected]" id="s004">John Seller"1000" from="12/03/16" to="12/03/18" no="M0020">State Membership"1000" from="12/03/16" to="12/03/18" no="M0002">GoldSt. Joseph, Missouri
I have the following C# classes
- class Advert2
- {
- public int? Id { get; set; }
- public DateTime? CreatedOn { get; set; }
- public DateTime? ExpiresOn { get; set; }
- public Aircraft MyAircraft { get; set; }
- public List
MySellers { get; set; } - public List
MyMemberships { get; set; } - public Advert2()
- {
- MySellers = new List
(); - MyMemberships = new List
(); - MyAircraft = new Aircraft();
- }
- }
- class Seller2
- {
- public int? Id { get; set; }
- public string SellerName { get; set; }
- public string Phone { get; set; }
- }
- class Membership
- {
- public int? Id { get; set; }
- public string MembershipNumber { get; set; }
- public DateTime? From { get; set; }
- public DateTime? To { get; set; }
- public String MemberType { get; set; }
- }
- class Aircraft {
- public string Make { get; set; }
- public string Model { get; set; }
- public decimal? Price { get; set; }
- public string Description { get; set; }
- }
Then i have used the following two methods to parse XML elements and attributes ( check for NULLs )
- public static class XMLCommons
- {
- public static string TryGetElementValue(this XElement parentEl, string elementName, string defaultValue = null)
- {
- var foundEl = parentEl.Element(elementName);
- if (foundEl != null)
- {
- return foundEl.Value;
- }
- return defaultValue;
- }
- public static string TryGetAttribtueValue(this XElement parentEl, string elementName, string attrName, string defaultValue = null)
- {
- var foundEl = parentEl.Element(elementName);
- if (foundEl != null) {
- //check attribute exists
- var foundAttr = foundEl.Attribute(attrName);
- if (foundAttr != null)
- {
- return foundAttr.Value;
- }
- }
- return defaultValue;
- }
- }
Then i have written the following code to read element/attributes on the XML, and populate data to the `Advert2` object structure
- var xmlPath2 = System.IO.Path.Combine("../../../data/" + "XMLFile2.xml");
- var xml2 = XDocument.Load(xmlPath2);
- var query2 = xml2.Root.Descendants("ad").Select(n => new Advert2 {
- Id = Convert.ToInt32(n.Parent.TryGetAttribtueValue("ad", "id")),
- CreatedOn = Convert.ToDateTime( n.Parent.TryGetAttribtueValue("ad", "createdon") ),
- ExpiresOn = Convert.ToDateTime(n.Parent.TryGetAttribtueValue("ad", "expireson")),
- MyAircraft = new Aircraft {
- Make = n.TryGetElementValue("make"),
- Model = n.TryGetElementValue("model"),
- Description = n.TryGetElementValue("description"),
- Price = Convert.ToDecimal( n.TryGetElementValue("price") ) },
- MySellers = new List
().Add( new Seller2 { - Id = Convert.ToInt32( n.TryGetAttribtueValue("seller","id") ),
- SellerName = n.TryGetElementValue("seller"),
- Phone = n.TryGetAttribtueValue("seller","phone")
- } )
- }).ToList();
but the issues is i get syntax errors when i tried to create objects to populate `MySellers` List.
Error:
- Severity Code Description Project File Line Suppression State
- Error CS0029 Cannot implicitly convert type 'void' to System.Collections.Generic.List
'
So it seems like i don't know how to populate those two collections `MySellers` and `MyMemberships`.
is there away to populate those two collections so i can create the `Averts2` Collection?
Rajanikant HawaldarPosted Jan 24, 2021, 8:59 AM