增加属性值时多次复制 XML 对象 [英] Duplicate XML object multiple times while incrementing attribute value
本文介绍了增加属性值时多次复制 XML 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 XML 代码:
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>
我想在以下条件下多次复制节点Group":1.除Parameter"节点外的所有子节点的Name"属性的值,应该是唯一的(它是增量的),如Group1,Group2...等
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.
以下是我需要的结果:
<?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)任何帮助真的很感激
Like wise i want duplicate complete node with several times. I am using VB.net (Visual Studio 2010) Any help really appreatiate
推荐答案
试试这个.这不是第一次完成这样的请求.我正在使用 xml linq 网络库.
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
这篇关于增加属性值时多次复制 XML 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文