Sybaris
Topic Author
Posts: 39
Joined: 21 Feb 2019, 10:13

Using LoadXaml() to load non-noesis objects

15 May 2021, 17:58

Hi everybody,

from WPF I'm used to using XamlReader.Load() to load files that contain non-UI elements, like for example configuration files. The xaml format enables one to write pretty complex configuration files.
I tried this with LoadXaml() and it crashes Unity. Sometimes i get an error message saying that the "XAML file is empty" (which is not the case) and then it produces that message endlessly.
So it's obviously not possible to load XAMLs that do not contain UI elements, right?

Günther
 
User avatar
sfernandez
Site Admin
Posts: 3014
Joined: 22 Dec 2011, 19:20

Re: Using LoadXaml() to load non-noesis objects

17 May 2021, 10:31

You should be able to load any kind of data defined in your XAML files as long as it uses classes defined in the Unity project.

Do you have those xaml assets referenced in your Unity scene somehow? Because if the assets are not part of the scene they are not included in the game and cannot be loaded. As we mentioned in our Unity tutorial you can manually add Xaml.Dependencies to another XAML if you want, or you can reference those configuration files (xaml assets) in any component of your scene.

Please let me know if that was the cause of your problem?
 
Sybaris
Topic Author
Posts: 39
Joined: 21 Feb 2019, 10:13

Re: Using LoadXaml() to load non-noesis objects

17 May 2021, 10:37

Hi Fernandez,

yes, that was probably one cause of the problem. The other problem was that a file with only a root tag and two attributes didn't load and made Unity crash, although it loaded fine under WPF.
We cannot preload the file because it is a settings file that should be editable by the user before app start.
I switched to a simple XML file - that should do it for now.
But thanks for your response!

Günther
 
User avatar
sfernandez
Site Admin
Posts: 3014
Joined: 22 Dec 2011, 19:20

Re: Using LoadXaml() to load non-noesis objects

17 May 2021, 11:44

The other problem was that a file with only a root tag and two attributes didn't load and made Unity crash, although it loaded fine under WPF.
Could you please attach an example of this, we shouldn't crash Unity editor never and we want to fix it.
We cannot preload the file because it is a settings file that should be editable by the user before app start.
Understood. But in that case you will probably be loading the file using a simple File.Open, right? Then another option could be to use XamlReader.Parse() to parse the contents of the file.
 
Sybaris
Topic Author
Posts: 39
Joined: 21 Feb 2019, 10:13

Re: Using LoadXaml() to load non-noesis objects

17 May 2021, 11:49

I'll see if I can build a small example of the crashing file. I don't have it anymore because I deleted it... ;-)

We are using the XmlSerializer at the moment:
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            T newObject;

            using (var reader = File.OpenText(path))
            {
                newObject = (T)serializer.Deserialize(reader);
            }
I wasn't aware that ther is a XamlReader.Parse() method - maybe I'll try that.
 
Sybaris
Topic Author
Posts: 39
Joined: 21 Feb 2019, 10:13

Re: Using LoadXaml() to load non-noesis objects

27 Apr 2022, 13:02

Hi again,

I wanted to deliver the crashing XAML:
<KarmaPackage
    xmlns="http:=//schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:y="clr-namespace:Karma.Config" />
The class is actually empty:
namespace Karma.Config
{
    [Serializable]
    public class KarmaPackage
    {
    }
}
This crashes Unity when I try to parse it with Noesis.GUI.ParseXaml().
It even crashes Unity when Unity touches the file, for example when a project is loaded and the file is parsed.
btw: I know that the namespaces in the XAML are wrong; this was just for testing purposes. But anyway, even if I use Karma.Config as default namespace the XamlReader still complains about not having a default namespace" and returns null. Any idea what I'm doing wrong?

Best regards,

G.
 
User avatar
sfernandez
Site Admin
Posts: 3014
Joined: 22 Dec 2011, 19:20

Re: Using LoadXaml() to load non-noesis objects

29 Apr 2022, 20:29

Thanks for the repro xaml.

It turns out that the problem was with single node xamls that doesn't define the default namespace. In your case you have a typo in the first xmlns address and wasn't recognized, it should be:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
We fixed the crash for the next release.
 
Sybaris
Topic Author
Posts: 39
Joined: 21 Feb 2019, 10:13

Re: Using LoadXaml() to load non-noesis objects

30 Apr 2022, 12:21

But what if I need an own default namespace? As I said, I want to read non-noesis objects which have their own namespace.

Thanks for your reply!

G.
 
User avatar
sfernandez
Site Admin
Posts: 3014
Joined: 22 Dec 2011, 19:20

Re: Using LoadXaml() to load non-noesis objects

02 May 2022, 12:29

Since 3.1.2 we support assigning non-null prefix to the default namespace, so you can use it for your own objects, so this should work:
<KarmaPackage
    xmlns:def="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns="clr-namespace:Karma.Config"/>
Although we should probably remove the requirement of declaring default namespace all along, as WPF doesn't need that, could you please report that in our bugtracker?
 
Sybaris
Topic Author
Posts: 39
Joined: 21 Feb 2019, 10:13

Re: Using LoadXaml() to load non-noesis objects

03 May 2022, 17:45

I wouldn't say that WPF doesn't use the default namespace (xmlns=) - it does use it.
In our example we have a configuration file containing dumb DTOs (POCOs), which are then wrapped in model objects with the same name but in a different namespace. If I wouldn't be able to set the default namespace, then it would be coincidence which namespace the parser takes.
So I wouldn't say it's a bug...
btw: it is working now. Thanks for the support!

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests