重复XML对象多次,同时增加属性值

问题描述:

我有以下XML代码:重复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> 

我想与下列条件复制节点“集团”几次:所有的“名称”属性的 1.Value除了“Parameter”节点之外的子节点应该是唯一的(它是增量式的),如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中) 任何帮助真的appreatiate

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

谢谢@jdweng 它很好用。 但它不会改变其他节点的属性值,如... 'numericDisplay name =“NumericDisplay3”' ' Viks

+0

通过其他更改更新了我的代码。 – jdweng

+0

谢谢你的工作很棒 – Viks