Search Unity

Xml and using a custom class?

Discussion in 'Scripting' started by Deleted User, Sep 23, 2016.

  1. Deleted User

    Deleted User

    Guest

    I don't understand how to make something like this work:

    <bookstore>
    <Book>
    <author>Giada De Laurentiis</author>
    </Book>
    </bookstore>


    I tried doing the following:

    [XmlElement]
    public Book book;
    public int test;


    public class Book
    {
    public string author;
    }


    but book is always null, test is not however. I guess [XmlElement] is incorrect but I don't know what to use.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's probably looking for <book> not <Book>. You can specify the element name

    Code (csharp):
    1.  
    2. [XmlElement("Book")]
    3. public Book book;
    4.  
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    That's probably it. I can't believe I missed that lol.

    edit: yes that was the problem. Thanks a lot, I just wish I didn't waste hours on such a simple mistake :/.