Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 忽略某些节点反序列化对象_C#_Xml_Xmlserializer - Fatal编程技术网

C# 忽略某些节点反序列化对象

C# 忽略某些节点反序列化对象,c#,xml,xmlserializer,C#,Xml,Xmlserializer,我有一个使用XmlSerializer序列化的对象。当尝试将此XML文件反序列化回对象时,由于某种原因它跳过了一个节点 <?xml version="1.0" encoding="utf-8"?> <Dialogue xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Conversations> &

我有一个使用XmlSerializer序列化的对象。当尝试将此XML文件反序列化回对象时,由于某种原因它跳过了一个节点

<?xml version="1.0" encoding="utf-8"?>
<Dialogue xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Conversations>
    <Conversation Id="993fed92-6917-4003-975f-78df4ca77257" Name="sdfsf">
      <TextNode Id="8ce1fba9-9daf-4145-b374-d8d7df6ca983" IsGraphEntryPoint="false" NodePositionX="1056" NodePositionY="341">
        <Speaker>None</Speaker>
        <Lines />
      </TextNode>
      <ChoiceNode Id="7c697932-dbde-47a2-b931-bc881811ef63" IsGraphEntryPoint="false" NodePositionX="783" NodePositionY="605">
        <Choices>
          <ChoiceOptionNode ChoiceId="cbe28c44-a535-4191-80b2-1bb3d1881cb7" PortName="Choice#1" ConnectsTo="82fefda3-f6d9-4462-a39e-a20c56f97bcb">
            <Lines>
              <ChoiceOptionLineNode>
                <LanguageId>42d97432-c027-416d-2aa8-1ec47ca1410e</LanguageId>
                <LanguageCode>NL</LanguageCode>
                <Text />
              </ChoiceOptionLineNode>
            </Lines>
          </ChoiceOptionNode>
        </Choices>
      </ChoiceNode>
    </Conversation>
  </Conversations>
</Dialogue>
所有TextNode都正确反序列化,但ChoiceNode似乎完全被忽略

对象本身:

public class ChoiceNode : BaseConversationNode
    {
        [XmlArray("Choices")]
        [XmlArrayItem(ElementName = "ChoiceOptionNode")]
        public List<ChoiceOptionNode> Choices;
    }

public class ChoiceOptionNode
    {
        [XmlAttribute]
        public Guid ChoiceId;
        [XmlAttribute]
        public string PortName;
        [XmlArray("Lines")]
        [XmlArrayItem(ElementName = "ChoiceOptionLineNode")]
        public List<ChoiceOptionLineNode> Lines;
        [XmlAttribute]
        public Guid ConnectsTo;
    }

public class ChoiceOptionLineNode
    {
        public Guid LanguageId;
        public string LanguageCode;
        public string Text;
    }

public abstract class BaseConversationNode
    {
        [XmlElement(ElementName = "Connections")]
        public List<Output> Connections = new List<Output>();
        [XmlIgnore]
        public GraphNode Node;
        [XmlAttribute]
        public Guid Id;
        [XmlAttribute]
        public bool IsGraphEntryPoint;
        [XmlIgnore]
        public Rect NodeRect;
        [XmlAttribute]
        public float NodePositionX;
        [XmlAttribute]
        public float NodePositionY;
        /// <summary>
        /// The hex color of the node window
        /// </summary>
        /// <returns></returns>
        [XmlIgnore]
        public string NodeColorHex => !String.IsNullOrEmpty(options?.NodeColor) ? options.NodeColor : "c1c1c1";
        [XmlIgnore]
        public float NodeWidth => options?.NodeWidth ?? 200;
        [XmlIgnore]
        public float NodeHeight => options?.NodeHeight ?? 150;

        [XmlIgnore]
        private NodeOptionsAttribute _options;
        [XmlIgnore]
        private NodeOptionsAttribute options
        {
            get
            {
                if (_options == null)
                {
                    NodeOptionsAttribute attr = (NodeOptionsAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(NodeOptionsAttribute));
                    _options = attr;
                }

                return _options;
            }
        }
        [XmlIgnore]
        public List<Language> Languages;
}

[XmlRoot(ElementName = "Connection")]
    public class Output
    {
        [XmlAttribute]
        public Guid ConnectsTo;
        [XmlAttribute]
        public string PortName;
        [XmlAttribute]
        public Direction PortDirection;
        [XmlAttribute]
        public Port.Capacity Capacity;
    }

 public class Dialogue
    {
        /// <summary>
        /// The list of Conversations
        /// </summary>
        [XmlArrayItem(ElementName = "Conversation")]
        public List<Conversation> Conversations;
        /// <summary>
        /// The list of Languages
        /// </summary>
        public List<Language> Languages;

        [XmlIgnore]
        private List<Type> nodeTypes;
        [XmlIgnore]
        public List<Type> NodeTypes
        {
            get
            {
                if (nodeTypes == null)
                {
                    nodeTypes = AppDomain.CurrentDomain.GetAssemblies()
                        .SelectMany(assembly =>
                            assembly.GetTypes())
                            .Where(type =>
                                type.IsSubclassOf(typeof(BaseConversationNode))
                                //&& type.CustomAttributes.Any(x => x.AttributeType != typeof(AutoGeneratedNodeAttribute))
                                && !type.Equals(typeof(StartNode))
                            )
                            .ToList();
                }

                return nodeTypes;
            }
        }
}      

public class Language
    {
        [XmlAttribute]
        public Guid Id;
        public string Name;
        public string Code;
    }  

public class Conversation
    {
        [XmlAttribute]
        public Guid Id;
        [XmlAttribute]
        public string Name;
        [XmlElement(typeof(StartNode), ElementName = "StartNode")]
        [XmlElement(typeof(TextNode), ElementName = "TextNode")]
        public List<BaseConversationNode> Nodes;
}
我觉得这很奇怪,因为我使用的XML正是XML序列化程序首先返回的内容。您可能认为它能够正确地反序列化它自己的序列化XML

我错过了什么

亲切问候,

-狂热的

编辑:忘记添加基类。
编辑2:我是个傻瓜,第一次编辑时忘了添加其他相关的类。

我注意到了我的错误。在以下方面:

[XmlElement(typeof(StartNode), ElementName = "StartNode")]
[XmlElement(typeof(TextNode), ElementName = "TextNode")]
public List<BaseConversationNode> Nodes;

这会正确地反序列化XML

[XmlElement(typeof(StartNode), ElementName = "StartNode")]
[XmlElement(typeof(TextNode), ElementName = "TextNode")]
[XmlElement(typeof(ChoiceNode), ElementName = "ChoiceNode")]
public List<BaseConversationNode> Nodes;