反序列化XML不会填充数组

本文关键字:填充 数组 XML 反序列化 | 更新日期: 2023-09-27 18:27:28

我有下面的XML和C#代码。Childconainer的数组没有被反序列化?我希望得到他们的列表,但我得到了0。

C#类和反序列化方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace MESMsgTester
{
    public class BscLineageResponse
    {
        public class BSCLineageResponse
        {
            public mes_message mes_message { get; set; }
        }
        public class mes_message
        {
            public string msg_environment { get; set; }
            public msg_header msg_header { get; set; }
            public msg_body msg_body { get; set; }
            public msg_error msg_error { get; set; }
        }
        public class msg_body
        {
            public Container Container { get; set; }
            public ChildContainer[] ChildContainer { get; set; }

        }
        public class msg_header
        {
            public string msg_type { get; set; }
            public int msg_stat { get; set; }
            public string msg_source { get; set; }
            public string msg_time { get; set; }
            public string plant_id { get; set; }
            public string msg_description { get; set; }
            public string msg_string { get; set; }
        }

        public class Container
        {
            public string Name { get; set; }            
        }
        public class ChildContainer
        {
            public string Name { get; set; }
            public string Level { get; set; }
        }

        public class TaskList
        {
            public string Name { get; set; }
            public string Revision { get; set; }
            public bool Optional { get; set; }
            public TaskItem TaskItem { get; set; }
        }
        public class TaskItem
        {
            public string Name { get; set; }
            public string Instruction { get; set; }
            public bool IsTlc { get; set; }
        }
        public class msg_error
        {
            public string error_source { get; set; }
            public string error_code { get; set; }
            public string error_string { get; set; }
        }
        public static BSCLineageResponse ReadBSCLineageResponse(string input)
        {
            BSCLineageResponse result = new BSCLineageResponse();
            XmlSerializer serializer = new XmlSerializer(typeof(mes_message));
            // Deserialize XML string into a class object
            result.mes_message = (mes_message)serializer.Deserialize(new StringReader(input));
            return result;
        }
    }
}

XML

  <?xml version="1.0" encoding="utf-8" ?> 
- <mes_message>
  <msg_environment>UUUUU</msg_environment> 
- <msg_header>
  <msg_type>VscLineageResponse</msg_type> 
  <msg_stat>1</msg_stat> 
  <msg_source>M590</msg_source> 
  <msg_time>2014/10/23 09:37:44</msg_time> 
  <msg_description /> 
  <msg_string>Inquiry completed successfully</msg_string> 
  </msg_header>
- <msg_body>
- <Container>
  <Name>70145862</Name> 
  </Container>
- <ChildContainer>
  <Name>MEDMESC-CLN0001</Name> 
  <Level>1</Level> 
  <ParentContainer>70145862</ParentContainer> 
  <RawMaterialFlag>true</RawMaterialFlag> 
- <Product>
  <Name>500503-002</Name> 
  <Revision>D</Revision> 
  <Description>XXX</Description> 
  <TestModelNumber /> 
  </Product>
- <MfgOrder>
  <Name /> 
  <VscSWR /> 
  </MfgOrder>
  <VscBatch>YYY</VscBatch> 
  <VscSerialNumber /> 
  <InRework>false</InRework> 
  <IsInProcess>true</IsInProcess> 
  <IsOnHold>false</IsOnHold> 
  <Status>2</Status> 
  <Qty>0</Qty> 
  <VscProductExpirationDate>2016/11/22</VscProductExpirationDate> 
  <LastCompletionDateGMT>2014/10/21 11:52:29</LastCompletionDateGMT> 
- <TaskList>
  <Name /> 
- <TaskItem>
  <Name /> 
  </TaskItem>
  </TaskList>
  </ChildContainer>
- <ChildContainer>
  <Name>Bay2-CLN0001</Name> 
  <Level>1</Level> 
  <ParentContainer>70145862</ParentContainer> 
  <RawMaterialFlag>true</RawMaterialFlag> 
- <Product>
  <Name>354325-001</Name> 
  <Revision>D</Revision> 
  <Description>Bay SHIPPING OUTER</Description> 
  <TestModelNumber /> 
  </Product>
- <MfgOrder>
  <Name /> 
  <VscSWR /> 
  </MfgOrder>
  <VscBatch>1171275900</VscBatch> 
  <VscSerialNumber /> 
  <InRework>false</InRework> 
  <IsInProcess>true</IsInProcess> 
  <IsOnHold>false</IsOnHold> 
  <Status>1</Status> 
  <Qty>34</Qty> 
  <VscProductExpirationDate>9999/12/30</VscProductExpirationDate> 
  <LastCompletionDateGMT>2014/08/06 11:32:18</LastCompletionDateGMT> 
- <TaskList>
  <Name /> 
- <TaskItem>
  <Name /> 
  </TaskItem>
  </TaskList>
  </ChildContainer>
- <ChildContainer>
  <Name>Base-CLN0001</Name> 
  <Level>1</Level> 
  <ParentContainer>70145862</ParentContainer> 
  <RawMaterialFlag>true</RawMaterialFlag> 
- <Product>
  <Name>354337-001</Name> 
  <Revision>B</Revision> 
  <Description>Base SHIPPING INNER Bay NXTDS</Description> 
  <TestModelNumber /> 
  </Product>
- <MfgOrder>
  <Name /> 
  <VscSWR /> 
  </MfgOrder>
  <VscBatch>1170654400</VscBatch> 
  <VscSerialNumber /> 
  <InRework>false</InRework> 
  <IsInProcess>true</IsInProcess> 
  <IsOnHold>false</IsOnHold> 
  <Status>1</Status> 
  <Qty>34</Qty> 
  <VscProductExpirationDate>9999/12/30</VscProductExpirationDate> 
  <LastCompletionDateGMT>2014/08/06 11:32:18</LastCompletionDateGMT> 
- <TaskList>
  <Name /> 
- <TaskItem>
  <Name /> 
  </TaskItem>
  </TaskList>
  </ChildContainer>
- <ChildContainer>
  <Name>Base2-CLN0001</Name> 
  <Level>1</Level> 
  <ParentContainer>70145862</ParentContainer> 
  <RawMaterialFlag>true</RawMaterialFlag> 
- <Product>
  <Name>354338-001</Name> 
  <Revision>C</Revision> 
  <Description>Base SHIPPING OUTER Bay NXTDS</Description> 
  <TestModelNumber /> 
  </Product>
- <MfgOrder>
  <Name /> 
  <VscSWR /> 
  </MfgOrder>
  <VscBatch>1170778400</VscBatch> 
  <VscSerialNumber /> 
  <InRework>false</InRework> 
  <IsInProcess>true</IsInProcess> 
  <IsOnHold>false</IsOnHold> 
  <Status>2</Status> 
  <Qty>0</Qty> 
  <VscProductExpirationDate>9999/12/30</VscProductExpirationDate> 
  <LastCompletionDateGMT>2014/10/02 10:36:04</LastCompletionDateGMT> 
- <TaskList>
  <Name /> 
- <TaskItem>
  <Name /> 
  </TaskItem>
  </TaskList>
  </ChildContainer>
- <ChildContainer>
  <Name>TOR2-CLN0001</Name> 
  <Level>1</Level> 
  <ParentContainer>70145862</ParentContainer> 
  <RawMaterialFlag>true</RawMaterialFlag> 
- <Product>
  <Name>357428-101</Name> 
  <Revision>L</Revision> 
  <Description>TORQUE WRENCH BI-DIRECT 6628 ORANGE</Description> 
  <TestModelNumber /> 
  </Product>
- <MfgOrder>
  <Name /> 
  <VscSWR /> 
  </MfgOrder>
  <VscBatch>1172658800</VscBatch> 
  <VscSerialNumber /> 
  <InRework>false</InRework> 
  <IsInProcess>true</IsInProcess> 
  <IsOnHold>false</IsOnHold> 
  <Status>2</Status> 
  <Qty>0</Qty> 
  <VscProductExpirationDate>9999/12/30</VscProductExpirationDate> 
  <LastCompletionDateGMT>2014/10/02 10:48:10</LastCompletionDateGMT> 
- <TaskList>
  <Name /> 
- <TaskItem>
  <Name /> 
  </TaskItem>
  </TaskList>
  </ChildContainer>   
- <ChildContainer>
  <Name>CORET2-CLN0001</Name> 
  <Level>2</Level> 
  <ParentContainer>70087213</ParentContainer> 
  <RawMaterialFlag>true</RawMaterialFlag> 
- <Product>
  <Name>402232-152</Name> 
  <Revision>E</Revision> 
  <Description>HEADER CORE PG DR Part</Description> 
  <TestModelNumber /> 
  </Product>
- <MfgOrder>
  <Name /> 
  <VscSWR /> 
  </MfgOrder>
  <VscBatch>16845435</VscBatch> 
  <VscSerialNumber /> 
  <InRework>false</InRework> 
  <IsInProcess>true</IsInProcess> 
  <IsOnHold>false</IsOnHold> 
  <Status>2</Status> 
  <Qty>0</Qty> 
  <VscProductExpirationDate>9999/12/30</VscProductExpirationDate> 
  <LastCompletionDateGMT>2014/10/08 21:04:44</LastCompletionDateGMT> 
- <TaskList>
  <Name /> 
- <TaskItem>
  <Name /> 
  </TaskItem>
  </TaskList>
  </ChildContainer>
- <ChildContainer>
  <Name>SEAP1-CLN0001</Name> 
  <Level>1</Level> 
  <ParentContainer>70145862</ParentContainer> 
  <RawMaterialFlag>true</RawMaterialFlag> 
- <Product>
  <Name>356909-066</Name> 
  <Revision>Q</Revision> 
  <Description>SEAL PLUG 0.105 TALL RIBBED</Description> 
  <TestModelNumber /> 
  </Product>
- <MfgOrder>
  <Name /> 
  <VscSWR /> 
  </MfgOrder>
  <VscBatch>1171928400</VscBatch> 
  <VscSerialNumber /> 
  <InRework>false</InRework> 
  <IsInProcess>true</IsInProcess> 
  <IsOnHold>false</IsOnHold> 
  <Status>1</Status> 
  <Qty>16</Qty> 
  <VscProductExpirationDate>9999/12/30</VscProductExpirationDate> 
  <LastCompletionDateGMT>2014/07/23 08:59:41</LastCompletionDateGMT> 
- <TaskList>
  <Name /> 
- <TaskItem>
  <Name /> 
  </TaskItem>
  </TaskList>
  </ChildContainer>
- <ChildContainer>
  <Name>NUSL-CLN0002</Name> 
  <Level>1</Level> 
  <ParentContainer>70145862</ParentContainer> 
  <RawMaterialFlag>true</RawMaterialFlag> 
- <Product>
  <Name>500324-003</Name> 
  <Revision>J</Revision> 
  <Description>ADHESIVE SLN RBR 6OZ SEMCO</Description> 
  <TestModelNumber /> 
  </Product>
- <MfgOrder>
  <Name /> 
  <VscSWR /> 
  </MfgOrder>
  <VscBatch>1172445300</VscBatch> 
  <VscSerialNumber /> 
  <InRework>false</InRework> 
  <IsInProcess>true</IsInProcess> 
  <IsOnHold>false</IsOnHold> 
  <Status>2</Status> 
  <Qty>0</Qty> 
  <VscProductExpirationDate>2015/06/16</VscProductExpirationDate> 
  <LastCompletionDateGMT>2014/07/24 16:17:41</LastCompletionDateGMT> 
- <TaskList>
  <Name /> 
- <TaskItem>
  <Name /> 
  </TaskItem>
  </TaskList>
  </ChildContainer>
  </msg_body>
  </mes_message>

反序列化XML不会填充数组

尝试使用属性来指定元素和属性。像这样的东西:

public class TaskItem
{
    public string Name { get; set; }
    public string Instruction { get; set; }
    public bool IsTlc { get; set; }
}
[XmlRoot("TaskList")]
public class TaskList
{
    public string Name { get; set; }
    public string Revision { get; set; }
    public bool Optional { get; set; }
    [XmlArray("TaskItems")]
    [XmlArrayItem("TaskItem")]
    public List<TaskItem> TaskItems { get; set; }
}

您已经用XMLAttribute修饰了属性,但它们是XML中的元素。因此,将所有XMLAttribute更改为XmlElement。

你看将XML反序列化为对象数组