How to keep parent element reference while parsing list of children in XML to Object in c#

微笑、不失礼 提交于 2019-12-11 15:33:59

问题


Want to keep trak of parent node in child object so that at any point I can go to immidiate parent and access its attributes and also can traverse siblings.

XML:

<Parent Name"P1">
    <Child Name="C1">
        <GChild Name="GC1"/>
        <GChild  Name="GC2"/>
    </Child>
    <Child Name="C2">
        <GChild  Name="GC3"/>
        <GChild  Name="GC4"/>
    </Child>
</Parent> 

Class:

[Serializable]
public class Parent
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlElement(ElementName = "Child")]
    public List<Child> Children { get; set; }

    public class Child
    {
        public Parent Parent { get; get;} //Want to know who is its parent.

        [XmlAttribute]
        public string Name { get; set; }

        [XmlElement(ElementName = "GChild")]
        public List<GChild> Children { get; set; }

        public class GChild
        {
            public Child Parent { get; set; } //Want to know who is its parent.

            [XmlAttribute]
            public string Name { get; set; }
        }
    }
}

Parse:

var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(new StringReader(xml));

Created the property named Parent of its parent type in all child elements but don't know what to do to assign it. Its null for now. What changes are needed to be done in object class?


回答1:


Create a flat table using Xml Linq. Creating hierarchical classes makes it harder to get parent/child relationships. :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication124
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Parent Name", typeof(string));
            dt.Columns.Add("Child Name", typeof(string));
            dt.Columns.Add("GChild", typeof(string));


            XDocument doc = XDocument.Load(FILENAME);

            foreach(XElement parent in doc.Descendants("Parent"))
            {
                string parentName = (string)parent.Attribute("Name");
                foreach(XElement child in parent.Elements("Child"))
                {
                    string childName = (string)child.Attribute("Name");
                    foreach (XElement gChild in child.Elements("GChild"))
                    {
                        string gChildStr = (string)gChild.Attribute("Name");

                        dt.Rows.Add(new object[] { parentName, childName, gChildStr });
                    }
                }
            }
        }
    }
}



回答2:


You could create custom collection to hold information about children. When items will be added to this collection you would set Parent property that should be ignored (XmlIgnoreAttribute) when serializeing/deserializing.
Read this. I think it is exactly wnat you search for.



来源:https://stackoverflow.com/questions/57355884/how-to-keep-parent-element-reference-while-parsing-list-of-children-in-xml-to-ob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!