eutychos tfar

eutychos tfar

  • NA
  • 2
  • 392

How to load XML data to a data structure?

Jan 23 2021 6:51 AM
I have the following XML document
  1. <?xml version = "1.0" encoding = "utf-8"?>  
  2. <flights_for_sale>  
  3. <ad id="0001" createdon ="11/02/20" expireson="12/02/20">  
  4. <aircraft id="A10">  
  5. <year> 1977 </year>  
  6. <make> <![CDATA[&c;]]> </make>  
  7. <model> Skyhawk </model>  
  8. <color> Light blue and white </color>  
  9. <description>  
  10. New paint, nearly new interior,  
  11. 685 hours SMOH, full IFR King avionics  
  12. </description>  
  13. <price> 23,495 </price>  
  14. </aircraft>  
  15. <seller id = "s001" phone="123-123-123"> Skyway Aircraft </seller>  
  16. <seller id = "s002" phone="123-123-222"> Boeing </seller>  
  17. <seller id = "s003" phone="123-123-233"> McDouglas </seller>  
  18. <membership id="1000" from="12/03/16" to="12/03/18" no="M0001">Silver</membership>  
  19. <membership id="1000" from="12/03/16" to="12/03/18" no="M0002">Gold</membership>  
  20. <membership id="1000" from="12/03/16" to="12/03/18" no="M0003">Platinum</membership>  
  21. <location>  
  22. <city> Rapid City, </city>  
  23. <state> South Dakota </state>  
  24. </location>  
  25. </ad>  
  26. <ad id="002" createdon ="11/05/20" expireson="12/05/20">  
  27. <aircraft>  
  28. <year> 1965 </year>  
  29. <make> &p; </make>  
  30. <model> Cherokee </model>  
  31. <color> Gold </color>  
  32. <description>  
  33. 240 hours SMOH, dual NAVCOMs, DME,  
  34. new Cleveland brakes, great shape  
  35. </description>  
  36. </aircraft>  
  37. <seller phone="555-333-2222" email="[email protected]" id="s004">John Seller</seller>  
  38. <membership id="1000" from="12/03/16" to="12/03/18" no="M0020">State Membership</membership>  
  39. <membership id="1000" from="12/03/16" to="12/03/18" no="M0002">Gold</membership>  
  40. <location>  
  41. <city> St. Joseph, </city>  
  42. <state> Missouri </state>  
  43. </location>  
  44. </ad>  
  45. </flights_for_sale>  
I have the following C# classes
  1. class Advert2  
  2. {  
  3. public int? Id { getset; }  
  4. public DateTime? CreatedOn { getset; }  
  5. public DateTime? ExpiresOn { getset; }  
  6. public Aircraft MyAircraft { getset; }  
  7. public List<Seller2> MySellers { getset; }  
  8. public List<Membership> MyMemberships { getset; }  
  9.   
  10. public Advert2()  
  11. {  
  12. MySellers = new List<Seller2>();  
  13. MyMemberships = new List<Membership>();  
  14. MyAircraft = new Aircraft();  
  15. }  
  16.   
  17.   
  18. }  
  19.   
  20. class Seller2  
  21. {  
  22. public int? Id { getset; }  
  23. public string SellerName { getset; }  
  24. public string Phone { getset; }  
  25. }  
  26.   
  27. class Membership  
  28. {  
  29. public int? Id { getset; }  
  30. public string MembershipNumber { getset; }  
  31.   
  32. public DateTime? From { getset; }  
  33.   
  34. public DateTime? To { getset; }  
  35.   
  36. public String MemberType { getset; }  
  37.   
  38. }  
  39.   
  40. class Aircraft {  
  41.   
  42. public string Make { getset; }  
  43. public string Model { getset; }  
  44.   
  45.   
  46. public decimal? Price { getset; }  
  47.   
  48. public string Description { getset; }  
  49. }  
Then i have used the following two methods to parse XML elements and attributes ( check for NULLs )
  1. public static class XMLCommons  
  2. {  
  3. public static string TryGetElementValue(this XElement parentEl, string elementName, string defaultValue = null)  
  4. {  
  5. var foundEl = parentEl.Element(elementName);  
  6.   
  7. if (foundEl != null)  
  8. {  
  9. return foundEl.Value;  
  10. }  
  11.   
  12. return defaultValue;  
  13. }  
  14.   
  15.   
  16.   
  17. public static string TryGetAttribtueValue(this XElement parentEl, string elementName, string attrName, string defaultValue = null)  
  18. {  
  19. var foundEl = parentEl.Element(elementName);  
  20.   
  21. if (foundEl != null) {  
  22. //check attribute exists  
  23.   
  24. var foundAttr = foundEl.Attribute(attrName);  
  25. if (foundAttr != null)  
  26. {  
  27. return foundAttr.Value;  
  28. }  
  29. }  
  30.   
  31. return defaultValue;  
  32. }  
  33.   
  34.   
  35. }  
 
Then i have written the following code to read element/attributes on the XML, and populate data to the `Advert2` object structure
  1. var xmlPath2 = System.IO.Path.Combine("../../../data/" + "XMLFile2.xml");  
  2. var xml2 = XDocument.Load(xmlPath2);  
  3. var query2 = xml2.Root.Descendants("ad").Select(n => new Advert2 {  
  4. Id = Convert.ToInt32(n.Parent.TryGetAttribtueValue("ad""id")),  
  5. CreatedOn = Convert.ToDateTime( n.Parent.TryGetAttribtueValue("ad""createdon") ),  
  6. ExpiresOn = Convert.ToDateTime(n.Parent.TryGetAttribtueValue("ad""expireson")),  
  7. MyAircraft = new Aircraft {  
  8. Make = n.TryGetElementValue("make"),  
  9. Model = n.TryGetElementValue("model"),  
  10. Description = n.TryGetElementValue("description"),  
  11. Price = Convert.ToDecimal( n.TryGetElementValue("price") ) },  
  12. MySellers = new List<Seller2>().Add( new Seller2 {  
  13. Id = Convert.ToInt32( n.TryGetAttribtueValue("seller","id") ),  
  14. SellerName = n.TryGetElementValue("seller"),  
  15. Phone = n.TryGetAttribtueValue("seller","phone")  
  16. } )  
  17. }).ToList();  
but the issues is i get syntax errors when i tried to create objects to populate `MySellers` List.
Error:
  1. Severity    Code    Description Project File    Line    Suppression State  
  2. Error   CS0029  Cannot implicitly convert type 'void' to System.Collections.Generic.List<XMLParsing.Seller2>'  
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?

Answers (1)