0

I'm having some problems with my WebAPI. I have followed the guide from Microsoft and it works: https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api?fbclid=IwAR0tEMDMX3URn2YO0pwnTTAbY2RuGkdu-HUobznac4Lwus6rOVPSeiX-lFs

Now I want to be able to get data from my XML file instead of the hardcoded values the guide use. I have tried to search for this already but not sure I find the right thing. How can I do this?

Code I have tried:

    public IEnumerable<Book> GetAllProducts()
    {

    XDocument doc = XDocument.Load("C:\\Users\\Name\\Desktop\\products.xml");

    foreach (XElement element in doc.Descendants("Catalog")

        .Descendants("Product"))

    {

        Product product = new Product();

        product.Id = element.Element("Id").Value;
        product.Name = element.Element("Name").Value;
        product.Category = element.Element("Category").Value;
        product.Added_Date = element.Element("Added_Date").Value;//this will not work because its not a string
        product.Price = element.Element("Price").Value;//this will not work because its not a string

        products.Add(product);

        }

        return products;
    }

XML Code:

<?xml version="1.0"?>
<catalog>
   <product id="P1">
      <name>Royal Gala</name>
      <category>Apple</category>
      <country>New Zeeland</country>
      <price>33.00</price>
      <added_date>2011-01-11</added_date>
      <description>This is a lovely red apple.</description>
   </product>
   <product id="P2">
      <name>Granny Smith</name>
      <category>Apple</category>
      <country>Australia</country>
      <price>33.00</price>
      <added_date>2013-12-25</added_date>
      <description>This is a lovely green apple.</description>
   </product>
</catalog>

1 Answer 1

0

You just need to parse string to a specific type

Product product = new Product();
//..
product.Added_Date = DateTime.Parse(element.Element("Added_Date").Value);
product.Price = double.Parse(element.Element("Price").Value); //or float, or decimal

And consider using Elements method instead of Descendants cause last one returns all children and their inner children and so on and Elements returns first level children

var productNodes = doc.Root.Elements("product"); //note this is case sensitive
foreach (XElement element in productNodes) {
    Product product = new Product();
    //..
}
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you! However, this was a side problem. I cannot get it to work at all.
It is working if I use this: Product[] products = new Product[] { new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }... blabla }; If I use the code I had in my post it doesn't show anything and also says NullReferenceException.
It still says the same as before: "An exception of type 'System.NullReferenceException' occurred"
@Jacob on which line an exception does occur?
book.Id = element.Element("Id").Value; and if I remove it it will be error on next row and countinuing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.