Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Performance concerns when loading and parsing XML?

Discussion in 'Scripting' started by Rosenborg, Mar 19, 2021.

  1. Rosenborg

    Rosenborg

    Joined:
    Feb 15, 2020
    Posts:
    2
    In C#, when loading an XML document via


    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("test.xml");


    Do I need to put in some waiting procedure before I can parse the XML nodes, to make sure the file has fully loaded?

    And are there any performance concerns parsing the XmlDocument as is, or should I be converting it to a C# class?

    So far both the loading and the parsing seem to be working fine with smaller XML files, but what if they become huge?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,198
    You don't have to wait, that's automatic. If the file is too large, the entire process will hang until it's been read.

    If it gets that large, you can use async versions of the methods to load off the main thread. You can google "C# async load xml" to get a million results.
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    As Baste points out, there may be issues.

    However, if you're just starting out and there is no reason to use XML, don't use XML. It is March 2021, please consider using something like JSON instead.

    "So you have a problem and you decide to use XML. Now you have two problems."

    Or heck, in Unity, put your data into ScriptableObjects... instantly editable, etc. Glorious for tightly-shaped predefined data that you can load in as small a chunk as you like, or all at once.
     
  4. Rosenborg

    Rosenborg

    Joined:
    Feb 15, 2020
    Posts:
    2
    Thanks for the responses :) I'll look into ScriptableObjects. The reason I'm leaning towards XML at this point is simply because it's more readable and editable for me, compared to JSON. But if it's generally something that causes problems, I may rethink.
     
    mopthrow likes this.
  5. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    If you have a lot of extra custom data that needs to be deserialized runtime with zero-to-minimum penalty then you better off with MessagePack. Both JSON and XML are slow and have overhead and allocations.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,198
    In what absurd world is

    <data>data</data>

    more readable than

    data

    ??????????