1

I try to get a list of all sound elements in the XML-File.

I got all marker but when i try to get all elements with the tag sound i don't get anything. The list is always 0.

Root Class

[XmlArray("markers")]
[XmlArrayItem("marker")]
public List<WaypointInfo> markers = new List<WaypointInfo>();

[XmlArray("pattern")]
[XmlArrayItem("sound")]
public List<SoundInfo> soundsInfo = new List<SoundInfo> ();

/// <summary>
/// The file extension.
/// </summary>
public static readonly string fileExtensionMp3 = "*.mp3";

public static FileLoader loadInformationXML(string filePath){

    //Define the rootclass
    XmlSerializer serializer = new XmlSerializer (typeof(FileLoader));

    //Create inputstream
    FileStream fl = new FileStream (filePath, FileMode.Open);

    //Extract the stream and save it into our root class
    FileLoader xmlData = serializer.Deserialize (fl) as FileLoader;

    //Close the inputstream
    fl.Close ();

    return xmlData;
}

The markers arraylist is correct but the other list of sounds is always null.

Class WaypointsInfo

using UnityEngine;
using System.Collections.Generic;
using System.Xml.Serialization;

public class WaypointInfo {

    [XmlAttribute("name")]
    public string markerName;

    public Pattern pattern = new Pattern();

}

Class Soundinfo

using UnityEngine;
using System.Xml.Serialization;
using System.Collections;

public class SoundInfo {

    /// <summary>
    /// The minimum distance that triggers this sound.
    /// </summary>
    [XmlAttribute("distance")]
    public float distance; 

    /// <summary>
    /// When this sound has been played the last time, in runtime milliseconds.
    /// </summary>
    public float lastPlayed;

    /// <summary>
    /// If this sound should be looped.
    /// </summary>
    public bool looped;
}

and the xml file

   <soundpackage>
    <markers>
        <marker name="bird">
            <pattern name="aPattern">
                <sound distance="10">bird.mp3</sound>
                <sound distance="10">bird.mp3</sound>
            </pattern>
        </marker>
    </markers>
</soundpackage>
Daniel
  • 446
  • 6
  • 7
Guchelkaben
  • 1,205
  • 1
  • 12
  • 18

1 Answers1

0

Does your root node have code like this xmlns=http;//somepath? Then you are dealing with namespaces. Namespaces have to be considered when you are getting zero results and you are sure you are referencing it correctly.

I'm not familiar with Xml Serialization. So I looked up namespaces for XML Serialization. Here's an example on another StackOverflow thread.

See this answer for a serialization answer with namespaces.

Community
  • 1
  • 1
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69