简体   繁体   English

C#获取具有相同名称的所有元素的列表 - XML反序列化

[英]C# get a list of all elements with the same name - XML Deserialize

I try to get a list of all sound elements in the XML-File. 我尝试获取XML文件中所有声音元素的列表。

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. 该列表始​​终为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. 标记arraylist是正确的,但另一个声音列表始终为空。

Class WaypointsInfo 类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 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 和xml文件

   <soundpackage>
    <markers>
        <marker name="bird">
            <pattern name="aPattern">
                <sound distance="10">bird.mp3</sound>
                <sound distance="10">bird.mp3</sound>
            </pattern>
        </marker>
    </markers>
</soundpackage>

Does your root node have code like this xmlns=http;//somepath ? 您的根节点是否具有类似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. 我不熟悉Xml序列化。 So I looked up namespaces for XML Serialization. 所以我查找了XML序列化的名称空间。 Here's an example on another StackOverflow thread. 这是另一个StackOverflow线程的示例。

See this answer for a serialization answer with namespaces. 有关命名空间的序列化答案,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将具有相同名称的不同xml元素反序列化为不同的C#类 - Deserialize different xml elements with same name into different C# classes 在C#中反序列化具有相同名称的多个XML元素 - Deserialize multiple XML elements with the same name in C# C#将具有属性的XML元素反序列化为List - C# Deserialize XML elements with attributes into List 通过C#中的XmlSerializer类反序列化具有相同名称的多个但“不同”的XML元素 - Deserialize multiple but “different” XML elements with the same name through XmlSerializer class in C# 如何在 c# 中使用具有相同名称但不同属性和结构的元素反序列化 XML - How to deserialize XML with elements with same name, but different attributes and structure in c# 通过C#中的XmlSerializer类反序列化多个具有相同名称的XML元素 - Deserialize multiple XML elements with the same name through XmlSerializer class in C# 在 C# 中反序列化具有不同类型名称的 XML 元素 - Deserialize XML elements with different type name in C# 如何从相同的名称xml C#获取所有值 - How to get all the values from the same Name xml c# c#:将未知的xml元素反序列化为数组或列表 - c#: deserialize unknown xml elements to an array or list C#将特定的xml元素反序列化到自定义列表中 - C# deserialize specific xml elements into custom list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM