复制和粘贴xml结构片段

时间:2019-04-04 08:53:00

标签: c# xml plc siemens

我是xml编辑的新手,我需要复制一个描述TIA Portal中变量的xml文件片段,并将其粘贴在同一文件的下面,这样我便得到了另一个相同的var,以后可以对其进行编辑。是否有严格的“复制”功能来复制整个结构片段以及其中的所有ChildNode和值?更像是文本复制粘贴吗?

在这里我要复制1:1的xml片段:

> names(getSheets(wb))[2]
[1] "name city"

父节点是:

  <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>

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

2 个答案:

答案 0 :(得分:0)

尝试使用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()));


        }
    }


}

答案 1 :(得分:0)

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

                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