0

I am wondering how I can map an XML file to C# objects.

This is my XML File:

<MyXML>
    <Suppliers>
        <Supplier 
            SupplierID="1" 
            SupplierName="Test" 
            SupplierContact="Test" 
            SupplierPhone="0612345678" 
            SupplierEmail="Test@Test.nl" 
            SupplierAddress="Test" 
            SupplierSuburb="Test" 
            SupplierPostcode="1234QE"></Supplier>
    </Suppliers>
    <Genres>
        <Genre 
        GenreID="1" 
        GenreName="Test"></Genre>
    </Genres>
    <Authors>
        <Author 
        AuthorID="1" 
        AuthorName="Test" 
        AuthorSurName="Test" 
        AuthorPlaceOfBirth="Test" 
        AuthorDoB="2000-07-06"></Author>
    </Authors>
    <Publishers>
        <Publisher 
        PublisherID="1" 
        PublisherName="Test" 
        PublisherContact="Test" 
        PublisherPhone="Test" 
        PublisherEmail="Test" 
        PublisherAddress="Test" 
        PublisherSuburb="Test" 
        PublisherPostcode="Test"></Publisher>
    </Publishers>
    <Inventories>
        <Inventory 
        InventoryID="1" 
        Title="Test" 
        WholeSale="Test" 
        MarkUp="Test" 
        Discount="Test" 
        QuantityInStock="Test" 
        GenreID="1" 
        SupplierID="1"></Inventory>
    </Inventories>
    <Books>
        <Book 
        InventoryID="1" 
        PublishYear="2000-10-10" 
        CoverType="Test" 
        PublisherID="1"></Book>
    </Books>
    <Author_Books>
        <Author_Book 
        AuthorID="1" 
        InventoryID="1"></Author_Book>
    </Author_Books>
    <Customers>
        <Customer 
        CustomerID="1" 
        CustomerName="Test" 
        CustomerSurname="Test" 
        CustomerPhone="0612345678" 
        CustomerEmail="Test" 
        CustomerAddress="Test" 
        CustomerPostcode="Test" 
        CustomerDiscount="100"></Customer>
    </Customers>
    <Receipts>
        <Receipt 
        ReceiptID="1" 
        CustomerID="1" 
        Date="2018-04-01"></Receipt>
    </Receipts>
    <Purchases>
        <Purchase 
        ReceiptID="1" 
        InventoryID="1" 
        Quantity="3" 
        AmountPaid="91"></Purchase>
    </Purchases>
</MyXML>

I just have no clue how I can map this to objects. This is because it contains data from multiple tables. I was thinking of datasets, but what is the best way of doing this? I have looked around, but no one really explains their awnser.

Brum
  • 629
  • 7
  • 27
  • 1
    Maybe you are looking for this https://stackoverflow.com/questions/220867/how-to-deal-with-xml-in-c-sharp ? – Pancabiel Nov 07 '18 at 18:04
  • 6
    This is an illegal file. An XML document must have *ONE* root node. Until you fix it, you can't do anything with it in C#. – paulsm4 Nov 07 '18 at 18:11
  • I changed it in the question, is that what you mean? – Brum Nov 07 '18 at 18:15
  • Thanks for that URL! This is what I was searching for! – Brum Nov 07 '18 at 18:16
  • 1
    As @paulsm4 pointed out, this is not a well-formed xml file. Deserialization is a piece of cake once we have a well-formed document. – Juan Simon Nov 07 '18 at 18:17
  • Your xml is still invalid.... – Rui Jarimba Nov 07 '18 at 18:18
  • Okay, I was looking in what I was doing wrong and noticed I made a horrible XML structure. I changed it now and even ran it trough a XML validator, this XML works. – Brum Nov 07 '18 at 18:26
  • 1
    you can get the help from this project: https://www.codeproject.com/Articles/1163664/Convert-XML-to-Csharp-Object – Hassaan Nov 07 '18 at 19:06
  • 1
    Thank you for the update. Possible duplicate of [How to map XML file content to C# object(s)](https://stackoverflow.com/questions/9336851/how-to-map-xml-file-content-to-c-sharp-objects) or [How to deal with XML in C#](https://stackoverflow.com/questions/220867/how-to-deal-with-xml-in-c-sharp). – paulsm4 Nov 07 '18 at 20:40

0 Answers0