简体   繁体   English

在XML序列化/反序列化过程中维护父子关系

[英]Maintaining Parent-Child Relationship During XML Serialization/Deserialization

Is it possible to maintain parent-child relationships when deserializing an XML file where there is a unique identifier in each the parent and child. 反序列化XML文件时,如果在父级和子级中每个都有唯一的标识符,是否可以保持父子关系。 For example, if the "Name" tag always contains a unique ID what would be the best means of maintaining the parent-child relationship in the following XML: 例如,如果“名称”标签始终包含唯一的ID,那么在以下XML中维护父子关系的最佳方法是:

<Building>
    <Name>Bldg 1</Name>
    <Room>
        <Name>Room 1</Name>
        <Table>
            <Name>Table 1</Name>
        </Table>
    </Room>
</Building>

In the class there is a property to house the parent node's text value in the "Name" tag, as well as the parent name's tag. 在该类中,有一个属性可在“名称”标签和父名称的标签中容纳父节点的文本值。 For example, the "Room" tag with name "Room 1" would have a property for the parent node's name, which is "Bldg 1" and a property for the parent node's tag, which would be "Building". 例如,名称为“ Room 1”的“ Room”标签将具有父节点名称的属性“ Bldg 1”,并具有父节点的标签属性“ Building”。 I don't have control over the schema, but this is a simplified version of the format the data is in. 我无法控制架构,但这是数据所在格式的简化版本。

I'm not against serialization. 我不反对序列化。 Just showing a different method using XML Linq 仅使用XML Linq显示另一种方法

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
             "<Root>" +
               "<Building>" +
                  "<Name>Bldg 1</Name>" +
                  "<Room>" +
                    "<Name>Room 1</Name>" +
                    "<Table>" +
                        "<Name>Table 1</Name>" +
                    "</Table>" +
                  "</Room>" +
                  "<Room>" +
                    "<Name>Room 2</Name>" +
                    "<Table>" +
                        "<Name>Table 1</Name>" +
                    "</Table>" +
                  "</Room>" +
               "</Building>" +
               "<Building>" +
                  "<Name>Bldg 2</Name>" +
                  "<Room>" +
                    "<Name>Room 1</Name>" +
                    "<Table>" +
                        "<Name>Table 1</Name>" +
                    "</Table>" +
                  "</Room>" +
                  "<Room>" +
                    "<Name>Room 2</Name>" +
                    "<Table>" +
                        "<Name>Table 1</Name>" +
                    "</Table>" +
                  "</Room>" +
               "</Building>" +
            "</Root>";

            XElement root = XElement.Parse(input);

            var results = root.Descendants("Building").Select(x => new
            {
                name = x.Element("Name").Value,
                rooms = x.Elements("Room").Select(y => new
                {
                    name = y.Element("Name").Value,
                    Tables = y.Elements("Table").FirstOrDefault().Element("Name").Value
                }).ToList()
            }).ToList();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM