Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Just what the title says.

I will attach a gif here. to show you what exactly I mean.


without running into outofmemoryexception

What I have tried:

........
Posted
Updated 14-Dec-16 18:14pm
v5
Comments
Patrice T 14-Sep-16 0:51am    
What is the size of XML ?
Maciej Los 14-Sep-16 2:36am    
Based on your video, seems you want to copy entire content of ObjectType node with DisplayName attribute "Function Block Program" into its parent node and then to change its Name attribute by adding 1, 2, 3, etc. at the end of it. Next, you want to do the same with OI node under ExportedObjects. Am i right?
Note, that xml file presented in video is differ than above. Why some nodes presented in video are duplicated?

1 solution

I'm not sure i understand you well...

I'd prefer to use Linq To Xml[^].

//define search criteria
string searchterm = "private";
StringComparison comparison = StringComparison.InvariantCulture;

//create instance of XDocument
XDocument xdoc = Xdocument.Load("FullFileName");

//define parent node  for ObjectType nodes
var otsparent = xdoc.Descendants("Types").SingleOrDefault();

//find ObjectType nodes Name starts with "private"
var ots2copy = xdoc.Descendants("ObjectType")
    .Where(x=>x.Attribute("Name").Value.StartsWith(searchterm, comparison))
    .ToList();

//loop through the list of nodes which meet criteria
int i = 1;
foreach(XElement original in ots2copy)
{
    //create deep copy of original node
    XElement copy = new XElement(original);
    //change Name attribute
    copy.Attribute("Name").Value += i.ToString();
    //add a copy to parent node
    otsparent.Add(copy);
    i++;
}

//...

//Save document at the end of process
//xdoc.Save("NewFullFileName.xml");


Result:
XML
<Types>
  <ObjectType Name="private.C53da99cf09a845c6ad14b0412d101132" DisplayName="Graphic" Description="Graphic" Icon="TGML.gif" Base="tgml.TGML" Abstract="0" Implements="" DefaultProperty="" Version="1"></ObjectType>
  <ObjectType Name="private.C99cf176f956241a6aabb850d8ff8e44f" DisplayName="Function Block Program" Description="Function block program" Icon="Menta.gif" Base="menta.Program" Abstract="0" Implements="" DefaultProperty="" Version="1">
    <Verbs>
      <Verb Type="Contain" Name="menta.Module" minOccurs="0" maxOccurs="2147483647" ContainType="DerivedAndBase" SubTarget="Config" />
      <Verb Type="Contain" Name="menta.Alarm" minOccurs="0" maxOccurs="2147483647" ContainType="DerivedAndBase" SubTarget="Config" />
      <Verb Type="Contain" Name="system.schedulecommon.ScheduleBase" minOccurs="0" maxOccurs="2147483647" ContainType="DerivedAndBase" SubTarget="Config" />
    </Verbs>
  </ObjectType>
  <ObjectType Name="private.C53da99cf09a845c6ad14b0412d1011321" DisplayName="Graphic" Description="Graphic" Icon="TGML.gif" Base="tgml.TGML" Abstract="0" Implements="" DefaultProperty="" Version="1"></ObjectType>
  <ObjectType Name="private.C99cf176f956241a6aabb850d8ff8e44f2" DisplayName="Function Block Program" Description="Function block program" Icon="Menta.gif" Base="menta.Program" Abstract="0" Implements="" DefaultProperty="" Version="1">
    <Verbs>
      <Verb Type="Contain" Name="menta.Module" minOccurs="0" maxOccurs="2147483647" ContainType="DerivedAndBase" SubTarget="Config" />
      <Verb Type="Contain" Name="menta.Alarm" minOccurs="0" maxOccurs="2147483647" ContainType="DerivedAndBase" SubTarget="Config" />
      <Verb Type="Contain" Name="system.schedulecommon.ScheduleBase" minOccurs="0" maxOccurs="2147483647" ContainType="DerivedAndBase" SubTarget="Config" />
    </Verbs>
  </ObjectType>
</Types>



On the same manner, you have to copy OI nodes. At this moment it isn't clear what nodes have to be copied.

For further details, please see:
LINQ to XML Overview (C#)[^]
.NET Language-Integrated Query for XML Data[^]
Basic Queries (LINQ to XML) (C#)[^]

Another way to achieve that is to use XmlDocument class[^], which provides a way to to load, validate, edit, add (even a clone[^] node), and position XML in a document.

Try! Good luck!

[EDIT]
To copy OI nodes:

var oisparent = xdoc.Descendants("ExportedObjects").SingleOrDefault();
var ois2copy = xdoc.Descendants("OI")
    .Where(x=>x.Descendants("OI").Count()>1)
    .Select((x, i)=> new
        {
            index = i,
            ois = x
        })
    .ToList();

int j = 1;
foreach(var indexednodes in ois2copy)
{
    //create deep copy of original node
    XElement copy = new XElement(indexednodes.ois);
    foreach(XElement oi in copy.Descendants("OI").Where(x=>x.Attribute("TYPE").Value.StartsWith(searchterm, comparison)))
    {
        //change Name attribute
        oi.Attribute("TYPE").Value += j.ToString();
    }
    //add a copy to parent node
    oisparent.Add(copy);
    j++;
}
 
Share this answer
 
v6
Comments
Member 12724052 14-Sep-16 21:53pm    
---
Maciej Los 15-Sep-16 1:49am    
Well, i showed you how to achieve that. Feel free to improve above code to your needs. I have not enough time to study your video again. You should provide enough details in text, for example: input and expected output. If there is some requirement, try to write it in psedo-code.
If my answer was helpful, please accept it (green button) to remove your question from unanswered list.
Cheers, Maciej
Member 12494414 15-Sep-16 6:38am    
sorry, I'll try not to waste your time.
Expected output for exportedObject Node

Not able to attach the the XML here: Please find a screenshot : http://i.imgur.com/fRIJAtgr.png
Maciej Los 15-Sep-16 14:10pm    
You have to post a xml content, not an xml file.
[EDIT]
I've seen that image. I understand why you're not able to find out solution. There are OI subnodes. I'll try to help you.
Maciej Los 15-Sep-16 15:22pm    
See updated answer ;)
Sorry, but results are not displayed. I suspect a bug on this forum...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900