Import and Read XML and CSV file Data in Xamarin Forms

Anonymous
2021-03-16T03:17:25.663+00:00

Hi How to import XML and CSV file in Xamarin forms and read Data in List. I have attached xml file screenshot. ID, Product, Amount mapping column name. ![78026-image.png][1] [1]: /api/attachments/78026-image.png?platform=QnA

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-03-16T09:32:59.12+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To read data from the .csv file, try using the 'CsvHelper' nuget package. And for the xml file, try using the XmlSerializer class to get the data.
    Check the link: https://stackoverflow.com/questions/9619324/how-to-read-xml-file-into-list

    You could place the files in the shared project and set the Build Action to Embedded Resource. Then load the files to access the data:

       var assembly = IntrospectionExtensions.GetTypeInfo(typeof(CurrentClass)).Assembly;  
       Stream stream = assembly.GetManifestResourceStream("TestApplication.temp.csv");  
       using (var reader = new System.IO.StreamReader(stream))  
       {  
           if (reader != null)  
           {  
               using (var csvReader = new CsvReader(reader, CultureInfo.CurrentCulture))  
               {  
                   while (csvReader.Read())  
                   {  
                       list.Add(new TestModel  
                       {  
                           costent = csvReader.GetField<string>(0),  
                           ...  
                       });  
                   }  
               }  
           }  
       }  
    

    Tutorial: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows#loading-files-embedded-as-resources

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.