复制并粘贴 xml 结构片段

Copying and pasting an xml structure fragment

我是 xml 编辑的新手,我需要复制描述 TIA Portal 中变量的 xml 文件的片段并将其粘贴到同一文件的正下方,所以我得到另一个我以后可以编辑的相同变量。是否有一个严格的 "copy" 函数来复制一个包含所有子节点和值的整个结构片段?更像是文本复制粘贴?

这是我要复制的 xml 片段 1:1 :

  <Hmi.Tag.Tag ID="40C" CompositionName="Tags">
    <AttributeList>
      <AcquisitionTriggerMode>Visible</AcquisitionTriggerMode>
      <AddressAccessMode>Symbolic</AddressAccessMode>
      <Coding>Binary</Coding>
      <ConfirmationType>None</ConfirmationType>
      <GmpRelevant>false</GmpRelevant>
      <JobNumber>0</JobNumber>
      <Length>2</Length>
      <LinearScaling>false</LinearScaling>
      <LogicalAddress />
      <MandatoryCommenting>false</MandatoryCommenting>
      <Name>Index_ManualControl[9]</Name>
      <Persistency>false</Persistency>
      <QualityCode>false</QualityCode>
      <ScalingHmiHigh>100</ScalingHmiHigh>
      <ScalingHmiLow>0</ScalingHmiLow>
      <ScalingPlcHigh>10</ScalingPlcHigh>
      <ScalingPlcLow>0</ScalingPlcLow>
      <StartValue />
      <SubstituteValue />
      <SubstituteValueUsage>None</SubstituteValueUsage>
      <Synchronization>false</Synchronization>
      <UpdateMode>ProjectWide</UpdateMode>
      <UseMultiplexing>false</UseMultiplexing>
    </AttributeList>
    <LinkList>
      <AcquisitionCycle TargetID="@OpenLink">
        <Name>1 s</Name>
      </AcquisitionCycle>
      <Connection TargetID="@OpenLink">
        <Name>OP170_HMI_Connection</Name>
      </Connection>
      <ControllerTag TargetID="@OpenLink">
        <Name>DB11000_OPXXX_StationData.ManualControl.Navigation.HMI_Index[9]</Name>
      </ControllerTag>
      <DataType TargetID="@OpenLink">
        <Name>Int</Name>
      </DataType>
      <HmiDataType TargetID="@OpenLink">
        <Name>Int</Name>
      </HmiDataType>
    </LinkList>
    <ObjectList>
      <MultilingualText ID="40E" CompositionName="Comment">
        <ObjectList>
          <MultilingualTextItem ID="40F" CompositionName="Items">
            <AttributeList>
              <Culture>en-US</Culture>
              <Text />
            </AttributeList>
          </MultilingualTextItem>
        </ObjectList>
      </MultilingualText>
      <MultilingualText ID="410" CompositionName="DisplayName">
        <ObjectList>
          <MultilingualTextItem ID="411" CompositionName="Items">
            <AttributeList>
              <Culture>en-US</Culture>
              <Text />
            </AttributeList>
          </MultilingualTextItem>
        </ObjectList>
      </MultilingualText>
      <MultilingualText ID="412" CompositionName="TagValue">
        <ObjectList>
          <MultilingualTextItem ID="413" CompositionName="Items">
            <AttributeList>
              <Culture>en-US</Culture>
              <Text />
            </AttributeList>
          </MultilingualTextItem>
        </ObjectList>
      </MultilingualText>
    </ObjectList>
  </Hmi.Tag.Tag>

父节点是:

<Hmi.Tag.TagTable ID="0">
  <AttributeList>
    <Name>ManualControl</Name>
  </AttributeList>
  <ObjectList>

    (...)

  </ObjectList>

我需要复制整个大结构并将它放在下面,所以在 (...) 里面。然后,我将使用 "for" 语句根据另一个文件的数组大小生成变量。

尝试使用 xml linq 进行以下操作:

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

namespace ConsoleApplication107
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement objectList = doc.Descendants("ObjectList").FirstOrDefault();

            XElement multilingualText = objectList.Element("MultilingualText");

            objectList.Add(XElement.Parse(multilingualText.ToString()));


        }
    }


}

非常感谢,所以我做了这样的事情:

                XDocument doc1 = XDocument.Load(folderName1 + "\" + TXTFiles[i]);
                XElement objectList = doc1.Descendants("ObjectList").FirstOrDefault();
                XElement HmiTag = objectList.Element("Hmi.Tag.Tag");
                objectList.Add(XElement.Parse(HmiTag.ToString()));

所以我想让它复制整个内容并粘贴到 (...) 中,它按照所附图片所示进行了操作。

Heres the file before using the code

The file after using the code, 15 objects, first one is copied and placed on the end