Viks Viks - 23 days ago 10
Vb.net Question

Duplicate XML object multiple times while incrementing attribute value

I have following XML code:

<?xml version="1.0" encoding="UTF-8"?>
<gfx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Gfx-SE8.xsd">
<displaySettings displayType="replace" position="useCurrentPosition" securityCode="*" backColor="white" />
<group name="Group2" visible="true" wallpaper="false" toolTipText="" exposeToVba="vbaControl" isReferenceObject="true">
<numericDisplay name="NumericDisplay2" height="20" width="252" left="259" top="305" visible="true">
<animations>
<animateVisibility expression="{#2}" expressionTrueState="visible"/>
</animations>
<connections>
<connection name="Value" expression="{#1}"/>
</connections>
</numericDisplay>
<text name="Text1" height="19" width="15" left="356" top="274"/>
<parameters>
<parameter name="#1" description="" value="Tag1"/>
<parameter name="#2" description="" value="Tag2"/>
</parameters>
</group>


I want to duplicate node "Group" several times with following conditions:
1.Value of "Name" Attribute of all child nodes except "Parameter" node, should be unique (It is incremental) like Group1,Group2...etc.

Following is the result that i need:

<?xml version="1.0" encoding="UTF-8"?>
<gfx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Gfx-SE8.xsd">
<displaySettings displayType="replace" position="useCurrentPosition" securityCode="*" backColor="white" />
<group name="Group3" visible="true" wallpaper="false" toolTipText="" exposeToVba="vbaControl" isReferenceObject="true">
<numericDisplay name="NumericDisplay3" height="20" width="252" left="259" top="305" visible="true">
<animations>
<animateVisibility expression="{#2}" expressionTrueState="visible"/>
</animations>
<connections>
<connection name="Value1" expression="{#1}"/>
</connections>
</numericDisplay>
<text name="Text2" height="19" width="15" left="356" top="274"/>
<parameters>
<parameter name="#1" description="" value="Tag1"/>
<parameter name="#2" description="" value="Tag2"/>
</parameters>
</group>
<group name="Group4" visible="true" wallpaper="false" toolTipText="" exposeToVba="vbaControl" isReferenceObject="true">
<numericDisplay name="NumericDisplay4" height="20" width="252" left="259" top="305" visible="true">
<animations>
<animateVisibility expression="{#2}" expressionTrueState="visible"/>
</animations>
<connections>
<connection name="Value2" expression="{#1}"/>
</connections>
</numericDisplay>
<text name="Text3" height="19" width="15" left="356" top="274"/>
<parameters>
<parameter name="#1" description="" value="Tag1"/>
<parameter name="#2" description="" value="Tag2"/>
</parameters>
</group>


Like wise i want duplicate complete node with several times.
I am using VB.net (Visual Studio 2010)
Any help really appreatiate

Answer

Try this. It is not the 1st time of done a request like this. I'm using xml linq Net Library.

Imports System.Text.RegularExpressions
Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Const REPEAT_COUNT = 5
    Sub Main()
        Dim doc As XDocument = XDocument.Load(FILENAME)
        Dim gfx As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "gfx").FirstOrDefault()


        Dim group As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "group").FirstOrDefault()
        Dim groupname As String = group.Attribute("name").Value
        Dim match As Match = Regex.Match(groupname, "(?'name'[^\d]+)(?'number'\d+)")
        Dim groupBaseName As String = match.Groups("name").Value

        Dim numericDisplay As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "numericDisplay").FirstOrDefault()
        Dim numericDisplayName As String = numericDisplay.Attribute("name").Value
        match = Regex.Match(numericDisplayName, "(?'name'[^\d]+)(?'number'\d+)")
        Dim numericDisplayBaseName As String = match.Groups("name").Value

        Dim text As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "text").FirstOrDefault()
        Dim textName As String = text.Attribute("name").Value
        match = Regex.Match(textName, "(?'name'[^\d]+)(?'number'\d+)")
        Dim textBaseName As String = match.Groups("name").Value


        Dim number As Integer = Integer.Parse(match.Groups("number").Value)
        Dim groupStr As String = group.ToString()
        For i = 0 To (REPEAT_COUNT - 1)
            number += 1
            Dim newGroup As XElement = XElement.Parse(groupStr)

            Dim newAttribute As XAttribute = newGroup.Attribute("name")
            newAttribute.SetValue(groupBaseName + number.ToString())

            Dim newNumericDisplay As XElement = newGroup.Descendants().Where(Function(x) x.Name.LocalName = "numericDisplay").FirstOrDefault()
            newAttribute = newNumericDisplay.Attribute("name")
            newAttribute.SetValue(numericDisplayBaseName + number.ToString())

            Dim newText As XElement = newGroup.Descendants().Where(Function(x) x.Name.LocalName = "text").FirstOrDefault()
            newAttribute = newText.Attribute("name")
            newAttribute.SetValue(textBaseName + number.ToString())


            gfx.Add(newGroup)
        Next i
    End Sub

End Module