【问题标题】:Duplicate XML object multiple times while incrementing attribute value在增加属性值时多次复制 XML 对象
【发布时间】:2017-03-25 18:40:45
【问题描述】:

我有以下 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.除“Parameter”节点外的所有子节点的“Name”属性值,如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) 任何帮助真的很感激

【问题讨论】:

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


    【解决方案1】:

    试试这个。这不是第一次完成这样的请求。我正在使用 xml linq 网络库。

    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
    

    【讨论】:

    • 谢谢@jdweng 它工作得很好。但它不会改变其他节点的属性值,比如... 'numericDisplay name="NumericDisplay3"' '
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2022-01-22
    • 1970-01-01
    • 2014-01-12
    • 2011-10-29
    相关资源
    最近更新 更多