xdocument : 移动元素到子节点

xdocument : moving elements to child node

我有以下xml

<root>
    <row>
        <MARKET>stringText</MARKET>
        <START_DT>20210529</START_DT>
        <END_DT>20210529</END_DT>
        <TIME_ZONE>GMT</TIME_ZONE>
        <ACTION>INSERT</ACTION>
        <NODE1>1</NODE1>
        <NODE2>1.3</NODE2>
    </row>
    <row>
        <MARKET>stringText</MARKET>
        <START_DT>20210529</START_DT>
        <END_DT>20210529</END_DT>
        <TIME_ZONE>GMT</TIME_ZONE>
        <ACTION>INSERT</ACTION>
        <NODE1>1</NODE1>
        <NODE2>1.2</NODE2>
    </row>
</root>

我想使用 c# xdocument 将一些元素转换为新的子节点。到目前为止,我是新手,尝试搜索如何将元素移动到子节点,但找不到任何东西。

<root>
    <row>
        <MARKET>stringText</MARKET>
        <START_DT>20210529</START_DT>
        <END_DT>20210529</END_DT>
        <TIME_ZONE>GMT</TIME_ZONE>
        <ACTION>INSERT</ACTION>
        <Sub_Node>
            <NODE1>1</NODE1>
            <NODE2>1.3</NODE2>
        </Sub_Node>
    </row>
    <row>
        <MARKET>stringText</MARKET>
        <START_DT>20210529</START_DT>
        <END_DT>20210529</END_DT>
        <TIME_ZONE>GMT</TIME_ZONE>
        <ACTION>INSERT</ACTION>
        <Sub_Node>
            <NODE1>1</NODE1>
            <NODE2>1.2</NODE2>
        </Sub_Node>
    </row>
</root>

我建议使用 System.Xml.Linq 命名空间。使用这些扩展,您可以搜索层次结构以找到合适的父节点或兄弟节点,然后使用 Functional Construction 创建新节点。

这是一种编写问题解决方案的方法。

class Program
{
    static void Main(string[] args)
    {
        var xel = XElement.Parse(source);
        var rows = xel.Elements("row").ToArray();
        foreach (var row in rows)
        {
            var node1 = row.Element("NODE1");
            var node2 = row.Element("NODE2");
            node1.Remove();
            node2.Remove();

            // Functional construction
            var sub_node =
                new XElement("Sub_Node", node1, node2);

            row.Element("ACTION").AddAfterSelf(sub_node);
        }

        Console.WriteLine(xel.ToString());
    }
    const string source =
  @"<root>
        <row>
            <MARKET>stringText</MARKET>
            <START_DT>20210529</START_DT>
            <END_DT>20210529</END_DT>
            <TIME_ZONE>GMT</TIME_ZONE>
            <ACTION>INSERT</ACTION>
            <NODE1>1</NODE1>
            <NODE2>1.3</NODE2>
        </row>
        <row>
            <MARKET>stringText</MARKET>
            <START_DT>20210529</START_DT>
            <END_DT>20210529</END_DT>
            <TIME_ZONE>GMT</TIME_ZONE>
            <ACTION>INSERT</ACTION>
            <NODE1>1</NODE1>
            <NODE2>1.2</NODE2>
        </row>
    </root>";
}

解决方案

using System.Xml.Linq;

var source = "<root>\r\n    <row>\r\n        <MARKET>stringText</MARKET>\r\n        <START_DT>20210529</START_DT>\r\n        <END_DT>20210529</END_DT>\r\n        <TIME_ZONE>GMT</TIME_ZONE>\r\n        <ACTION>INSERT</ACTION>\r\n        <NODE1>1</NODE1>\r\n        <NODE2>1.3</NODE2>\r\n    </row>\r\n    <row>\r\n        <MARKET>stringText</MARKET>\r\n        <START_DT>20210529</START_DT>\r\n        <END_DT>20210529</END_DT>\r\n        <TIME_ZONE>GMT</TIME_ZONE>\r\n        <ACTION>INSERT</ACTION>\r\n        <NODE1>1</NODE1>\r\n        <NODE2>1.2</NODE2>\r\n    </row>\r\n</root>";
XDocument sourceXDoc;

using var stringReader = new StringReader(source);
sourceXDoc = XDocument.Load(stringReader);

foreach (var rowElement in sourceXDoc.Descendants().Where(_=>_.Name.LocalName == "row"))
{
    var node1 = rowElement.Element("NODE1");
    var node2 = rowElement.Element("NODE2");

    var subNode = new XElement("Sub_node");

    if (node1 != null)
    {
        subNode.Add(new XElement("SEGMENT_NUMBER", node1.Value));
        node1?.Remove();
    }

    if (node2 != null)
    {
        subNode.Add(new XElement("QUANTITY", node2.Value));
        node2?.Remove();
    }

    rowElement.Add(subNode);
}

Console.WriteLine(sourceXDoc.ToString());

输出

<root>
  <row>
    <MARKET>stringText</MARKET>
    <START_DT>20210529</START_DT>
    <END_DT>20210529</END_DT>
    <TIME_ZONE>GMT</TIME_ZONE>
    <ACTION>INSERT</ACTION>
    <Sub_node>
      <SEGMENT_NUMBER>1</SEGMENT_NUMBER>
      <QUANTITY>1.3</QUANTITY>
    </Sub_node>
  </row>
  <row>
    <MARKET>stringText</MARKET>
    <START_DT>20210529</START_DT>
    <END_DT>20210529</END_DT>
    <TIME_ZONE>GMT</TIME_ZONE>
    <ACTION>INSERT</ACTION>
    <Sub_node>
      <SEGMENT_NUMBER>1</SEGMENT_NUMBER>
      <QUANTITY>1.2</QUANTITY>
    </Sub_node>
  </row>
</root>