c# - 从结果集中创建一个 xml 节点树,但使用 linq 或表达式组合重复的属性

标签 c# asp.net xml linq

您好,我有以下格式的数据集:

NodeID | NodeName | NodeAttribute1 | NodeAttribute2
   1   |    n1    |       1        |      null
   2   |    n2    |       1        |      null
   3   |    n3    |       1        |        1
   4   |    n4    |       2        |        2
   5   |    n5    |       2        |        3
   6   |    n6    |       2        |        4

基本上,属性决定了节点在虚拟树中的位置。我希望结果看起来像这样:

<NodeTree>
    <ParentNodeType1 NodeAttribute1 = 1> 
        <node name="n1" nodeID=1>
        <node name="n2" nodeID=2>
        <ParentNodeType2 NodeAttribute2 = 1>
            <node name = "n3" nodeID = 3>
        </ParentNodeType2>
    </ParentNodeType1>
    <ParentNodeType1 NodeAttribute1 = 2> 
        <ParentNodeType2 NodeAttribute2 = 3>
            <node name = "n4" nodeID=4>
        </ParentNodeType2>
        <ParentNodeType2 NodeAttribute2 = 4>
            <node name = "n5" nodeID=5>
            <node name = "n6" nodeID=6>
        </ParentNodeType2>
    </ParentNodeType1>
</NodeTree>

因此 n1 和 n2 在 parent1 中,但 n3 更深一层,因为第二个属性不为空。任何人对如何使用 linq 或类似的东西来完成这件事有任何想法吗?

最佳答案

抱歉,我试图理解这一点,但我失败了。

您的数据似乎没有很好地描述(属性是什么?为什么不希望在 XML 中映射层次结构?ParentNodeType1/2 应该是什么?NodeAttribute2 似乎是父节点 ID ,但在 XML 节点中,n5 和 n6 都在 <ParentNodeType2 NodeAttribute2 = 4> 下,我不够聪明,无法检测到那里的逻辑:)

尽管如此,为了帮助您,以下是我认为可能有用的内容,您可以简单地使其适应您的“要求”,而无需我知道(眨眼,眨眼)

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

namespace X 
{ 
    static class Y 
    {
        public static int Main(string[] args)
        {
            var data = new Record[] {
                new Record(1, "n1", 1, null),
                    new Record(2, "n2", 1, null),
                    new Record(3, "n3", 1, 1),
                    new Record(4, "n4", 2, 2),
                    new Record(5, "n5", 2, 3),
                    new Record(6, "n6", 2, 4),
            };

            Func<Record, XElement> subtree;
            subtree = node => new XElement("node", 
                    new XAttribute("name", node.NodeName),
                    new XAttribute("nodeID", node.NodeID),
                    new XAttribute("NodeAttribute1", node.NodeAttribute1),
                    data.ChildrenOf(node.NodeID).Select(subtree));

            XElement xml = new XElement("NodeTree",
                    data.Where(root => root.NodeAttribute2 == null)
                    .Select(subtree));

            Console.WriteLine(xml);
            return 0;
        }

        struct Record
        {
            public readonly int NodeID;
            public readonly string NodeName;
            public readonly int  NodeAttribute1; //level?
            public readonly int? NodeAttribute2; //parent

            internal Record(int id, string name, int at1, int? at2) 
            {
                NodeID = id;
                NodeName = name;
                NodeAttribute1 = at1;
                NodeAttribute2 = at2;
            }
        }

        private static IEnumerable<Record> ChildrenOf(this IEnumerable<Record> data, int? parentId)
        {
            return data.Where(child => child.NodeAttribute2 == parentId);
        }
    }
}

哦,以防万一,这是输出

<NodeTree>
  <node name="n1" nodeID="1" NodeAttribute1="1">
    <node name="n3" nodeID="3" NodeAttribute1="1">
      <node name="n5" nodeID="5" NodeAttribute1="2" />
    </node>
  </node>
  <node name="n2" nodeID="2" NodeAttribute1="1">
    <node name="n4" nodeID="4" NodeAttribute1="2">
      <node name="n6" nodeID="6" NodeAttribute1="2" />
    </node>
  </node>
</NodeTree>

关于c# - 从结果集中创建一个 xml 节点树,但使用 linq 或表达式组合重复的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5585099/

相关文章:

c# - Microsoft Office Interop Word 读取页眉和脚注

c# - Asp vnext IServiceCollection 存在于两个命名空间

c# - 从全局 asax 获取页面的完整 url

xml - 如何在 powershell 中使用带有命名空间的 xpath 访问元素?

xml - 使用 Go 解析 XML,具有多个小写元素

java - 尝试解析 xml 文件但在部署的 GAE 中出现 java.security.AccessControlException

c# - CLR 中 castclass 操作码的用途是什么?

c# - 更新通用存储库中的实体时出错

c# - ASP.NET - Razor 从模型中获取图像

asp.net - ASP .NET MVC 在工作期间进行身份验证和保持用户身份验证的最佳实践