反序列化 XML:是否可以导入更多包含列表的对象,其中列表元素是相同的对象?

Deserializing XML: Is it possible to import more Object containing Lists, where the List Elements are the same objects?

我序列化了一些对象,所有对象都有一个"LinkObject"的列表。这些列表共享其中的一些 "Linkobjects"。如果我反序列化,那么所有列表元素都将是独立的、不同的对象。

I have a list of "OperationsObjects":
public class OperationObject
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        public string Physname { get; set; }
        public string JournalID { get; set; }
        public List<ParameterObject> ParameterObjectList = new List<ParameterObject>();
        public List<ConditionObject> ConditionObjectList = new List<ConditionObject>();
        public List<LinkObject> ChildLinkObjectList = new List<LinkObject>();
    }

public class LinkObject
    {
        public int? Number { get; set; }
        public string LogicType { get; set; }
        public string PrimaryID { get; set; }
        public string SecondaryID { get; set; }
    }

如果我序列化然后我得到一些类似的 XML 行:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfOperationObjects xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OperationObjectList>
    <OperationObject Name="step forward">
      <ParameterObjectList />
      <ConditionObjectList />
      <ChildLinkObjectList>
        <LinkObject>
          <Number>0</Number>
          <LogicType>And</LogicType>
          <PrimaryID>GanttOperationObject[gantt_1]</PrimaryID>
          <SecondaryID>GanttOperationObject[gantt_2]</SecondaryID>
        </LinkObject>
        <LinkObject>
          <Number>2</Number>
          <LogicType>And</LogicType>
          <PrimaryID>GanttOperationObject[gantt_3]</PrimaryID>
          <SecondaryID>GanttOperationObject[gantt_1]</SecondaryID>
        </LinkObject>
      </ChildLinkObjectList>
      <Physname>Program-Parameters</Physname>
      <PhysType>NXOpen.Mechatronics.ProxyObject</PhysType>
      <Duration>2</Duration>
      <StartTime>1</StartTime>
      <OperationType>Simple</OperationType>
      <JournalID>GanttOperationObject[gantt_1]</JournalID>
    </OperationObject>
    <OperationObject Name="118 Component M8">
      <ParameterObjectList />
      <ConditionObjectList />
      <ChildLinkObjectList>
        <LinkObject>
          <Number>0</Number>
          <LogicType>And</LogicType>
          <PrimaryID>GanttOperationObject[gantt_1]</PrimaryID>
          <SecondaryID>GanttOperationObject[gantt_2]</SecondaryID>
        </LinkObject>
        <LinkObject>
          <Number>1</Number>
          <LogicType>And</LogicType>
          <PrimaryID>GanttOperationObject[gantt_2]</PrimaryID>
          <SecondaryID>GanttOperationObject[gantt_4]</SecondaryID>
        </LinkObject>
      </ChildLinkObjectList>
      <Duration>1</Duration>
      <StartTime>3</StartTime>
      <OperationType>Simple</OperationType>
      <JournalID>GanttOperationObject[gantt_2]</JournalID>
    </OperationObject>

所以我想为编号为 0 的 LinkObjects 使用相同的对象。 实际上我想在我的 OperationObject Name="step forward" 中编辑 Linkobject,并且我期望 LinkObject 也会在 OperationObject Name="118 Component M8"

中编辑

你不能自动完成。但是你应该在反序列化后手动完成。您可以收集所有唯一的 LinkObjects 以分离集合,并通过 Id(在您的情况下它是 Number 值)将每个相关的 LinkObject 替换为 OperationObject。
有代码示例。我使用了 json 序列化程序,但实际上这并不重要。

List<TestParent> parents = new List<TestParent>();
            TestChild child = new TestChild() { Name = "Test" };
            //add to parent class with the same child class;
            parents.Add(new TestParent() { Child = new List<TestChild>() { child } });
            parents.Add(new TestParent() { Child = new List<TestChild>() { child } });
            String data = JsonConvert.SerializeObject(parents);
            List<TestParent> deserializedData = JsonConvert.DeserializeObject<List<TestParent>>(data);
            var comparer = new ChildComparer();
            List<TestChild> brokenLinkCollection = deserializedData.SelectMany(x => x.Child).Distinct().ToList();
            // 2 Child with the same Name
            List<TestChild> uniqueCollection = deserializedData.SelectMany(x => x.Child).Distinct(comparer).ToList();

            var processedChild = deserializedData.Select(x => x.Child).ToList();

            processedChild.ForEach(x =>
            {
                var substitutedCollection = uniqueCollection.Where( uc => x.Contains(uc, comparer)).ToList(); 
                x.Clear();
                x.AddRange(substitutedCollection);
            });

            List<TestChild> resoredCollection = deserializedData.SelectMany(x => x.Child).Distinct().ToList();
            // 1 Child is found due to linking to one memory object

用过 类:

 class ChildComparer : EqualityComparer<TestChild>
    {
        public override bool Equals(TestChild b1, TestChild b2)
        {
            if (b1 == null && b2 == null)
                return true;
            else if (b1 == null || b2 == null)
                return false;

            return (b1.Name == b2.Name);
        }
        public override int GetHashCode(TestChild bx)
        {
            return bx.Name.GetHashCode();
        }
    }
    public class TestChild
    {
        public string Name { set; get; }
    }
    public class TestParent
    {
        public List<TestChild> Child { set; get; }
    }