简体   繁体   English

反序列化 XML 元素数组

[英]Deserialize XML Array of Elements

So, deserializing is working however I have some xml like this:所以,反序列化工作但是我有一些 xml 像这样:

<File>
  <Stuff>stuff</Stuff>
  <Devices>
    <Device Type="1">
      <Number>1</Number>
    </Device>
    <Device Type="2">
      <Number>2</Number>
    </Device>
  </Devices>
</File>

When it gets to the array of devices... the first device is the only one that is populated into an object.当它到达设备阵列时……第一个设备是唯一一个填充到 object 中的设备。 Here are the classes:以下是课程:

            XmlSerializer deserializer;
            XmlRootAttribute xRoot = new XmlRootAttribute();
            FileStream stream = new FileStream(CONFIG_PATH, FileMode.Open);
            XmlReader reader = new XmlTextReader(stream);

            // Details configuration area.
            xRoot.ElementName = "File";
            xRoot.IsNullable = true;
            deserializer = new XmlSerializer(typeof(File), xRoot);
            Configuration = (File)deserializer.Deserialize(reader);

[Serializable()]
[XmlRoot(ElementName = "File", IsNullable = true)]
public sealed class File
{
    [XmlElement("Devices")]
    public Devices Devices { get; set; }
}

// <summary>
/// Configuration device elements.
/// </summary>
[Serializable()]
[XmlRoot("Devices", IsNullable = true)]
public sealed class Devices
{
    [XmlElement("Device")]
    public DeviceElements[] DeviceElements { get; set; }
}

/// <summary>
/// Configuration Devices.
/// </summary>
[Serializable()]
[XmlRoot("Device", IsNullable = true)]
public sealed class DeviceElements
{
    [XmlAttribute("Type")]
    public string DeviceType { get; set; }

    [XmlElement("Number")]
    public int DeviceNumber { get; set; }
}

Again, only the first Device is populated, I've played with XmlArray And XmlArrayItem however neither of them were even giving me the first value.同样,只填充了第一个设备,我玩过 XmlArray 和 XmlArrayItem 但是他们都没有给我第一个值。 Any suggestions?有什么建议么?

You must be doing something wrong at another place.你一定在另一个地方做错了什么。 Just using the Devices part of your XML:只需使用 XML 的Devices部分:

  <Devices>
    <Device Type="1">
      <Number>1</Number>
    </Device>
    <Device Type="2">
      <Number>2</Number>
    </Device>
  </Devices>

I was able to successfully deserialize a Devices class instance with multiple DeviceElements children given your classes above using similar code as you used in your last question:我能够成功地反序列化具有多个DeviceElements子级的Devices class 实例,因为上面的类使用与上一个问题中使用的代码类似的代码:

FileStream stream = new FileStream("test.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(stream);

XmlSerializer deserializer = new XmlSerializer(typeof(Devices));
var devicesResult = (Devices)deserializer.Deserialize(reader);

Edit:编辑:

Using the full XML:使用完整的 XML:

<File>
  <Stuff>stuff</Stuff>
  <Devices>
    <Device Type="1">
      <Number>1</Number>
    </Device>
    <Device Type="2">
      <Number>2</Number>
    </Device>
  </Devices>
</File>

This successfully deserializes for me:这成功地为我反序列化:

FileStream stream = new FileStream("test.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(stream);

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "File";
xRoot.IsNullable = true;

XmlSerializer deserializer = new XmlSerializer(typeof(File), xRoot);
var fileResult = (File)deserializer.Deserialize(reader);

On a side note - you probably shouldn't name your class the same as one already in the .NET framework ( System.IO.File ), also in above code example setting the Xml root is not required since you use the same name anyway, just leave this out: On a side note - you probably shouldn't name your class the same as one already in the .NET framework ( System.IO.File ), also in above code example setting the Xml root is not required since you use the same name anyway,把这个留在外面:

FileStream stream = new FileStream("test.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(stream);
XmlSerializer deserializer = new XmlSerializer(typeof(File));
var fileResult = (File)deserializer.Deserialize(reader);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM