在递增属性值的同时多次复制XML对象

时间:2016-11-11 14:00:02

标签: xml vb.net visual-studio visual-studio-2010

我有以下XML代码:

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

我想复制节点&#34; Group&#34;几次以下条件: 1.姓名&#34;的价值所有子节点的属性,除了&#34;参数&#34; node,应该是唯一的(它是增量的),如Group1,Group2 ......等。

以下是我需要的结果:

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

同样聪明的我想多次重复完整节点。 我使用的是VB.net(Visual Studio 2010) 任何真正有用的帮助

1 个答案:

答案 0 :(得分:0)

试试这个。这不是第一次完成这样的请求。我正在使用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