首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反序列化XML:是否可以导入更多包含列表的对象,其中列表元素是相同的对象?

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

Stack Overflow用户
提问于 2019-05-16 13:49:01
回答 1查看 40关注 0票数 0

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

代码语言:javascript
复制
I have a list of "OperationsObjects":
代码语言:javascript
复制
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行:

代码语言:javascript
复制
<?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>

因此,我希望LinkObjects有相同的对象,编号为0。实际上,我想编辑OperationObject Name=中的链接对象“前进一步”,我期望Linkobject也会在OperationObject Name="118 Component M8“中进行编辑。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-16 14:11:52

你不能自动这么做。但是您应该在反序列化之后手动执行。您可以收集所有唯一的LinkObjects来分离集合,并通过Id将每个相关的LinkObject替换为OperationObject (在您的示例中是Number值)。

有代码示例。我曾经使用过json序列化程序,但实际上它并不重要。

代码语言:javascript
复制
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

用过的课程:

代码语言:javascript
复制
 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; }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56170135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档