I'm not sure i understand you well...
I'd prefer to use
Linq To Xml[
^].
string searchterm = "private";
StringComparison comparison = StringComparison.InvariantCulture;
XDocument xdoc = Xdocument.Load("FullFileName");
var otsparent = xdoc.Descendants("Types").SingleOrDefault();
var ots2copy = xdoc.Descendants("ObjectType")
.Where(x=>x.Attribute("Name").Value.StartsWith(searchterm, comparison))
.ToList();
int i = 1;
foreach(XElement original in ots2copy)
{
XElement copy = new XElement(original);
copy.Attribute("Name").Value += i.ToString();
otsparent.Add(copy);
i++;
}
Result:
<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)
{
XElement copy = new XElement(indexednodes.ois);
foreach(XElement oi in copy.Descendants("OI").Where(x=>x.Attribute("TYPE").Value.StartsWith(searchterm, comparison)))
{
oi.Attribute("TYPE").Value += j.ToString();
}
oisparent.Add(copy);
j++;
}