0

I'm new to xml editing and I need to copy a fragment of a xml file that describes a variable in TIA Portal and paste it just underneath in the same file, so I get another same var that I can later edit. Is there a strict "copy" function for duplicating a whole structure fragment with all the ChildNodes and values inside? Just more like a text copy-paste?

Heres the xml fragment that i want to copy 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>

The ParentNodes are:

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

    (...)

  </ObjectList>

I need to copy that whole big structure and put it exactly underneath, so inside the (...) . I will then use "for" statement to generate variables depending on an array size from another file.

memek
  • 23
  • 3

2 Answers2

0

Try following using 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()));


        }
    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

Thank you very much, so i did something like this:

                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()));

So i wanted it to copy the whole and paste inside the (...), and it did it as shown on the attached images.

Heres the file before using the code

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

memek
  • 23
  • 3