簡體   English   中英

如何通過更改Vb.net中的元素名稱從xml文件復制完整的節點並創建多個節點

[英]How to copy complete node from xml file and create multiple node by changing names of elements in Vb.net

我想從xml文件復制完整的節點,並使用該節點創建新的xml文件。

假設節點由對象組成,例如button-1,Poygon-1 ... etc
我想通過將名稱更改為.... Button-2,Polygon-2..etc來創建多個節點。
就像我的節點名稱是
組名稱=“ GO_P_AIn1”,我想創建“ GO_P_AIn2”,“ GO_P_AIn3”,“ GO_P_AIn3”
我想在VB.net中創建函數...每次將節點作為輸入並使用相同的節點(僅通過更改對象名稱)在輸出xml文件中創建節點數。

<group name="GO_P_AIn1" visible="true" wallpaper="false" toolTipText="/*S:0 {#102.Cfg_Tag}*/: /*S:0 #102.Cfg_Desc*/" exposeToVba="notExposed" isReferenceObject="true" linkSize="true" linkConnections="true" linkAnimations="linkWithExpression" linkBaseObject="(RA-BAS) P_AIn Graphics Library.GO_P_AIn" linkToolTipText="true">
    <rectangle name="AIN_AlarmPolygon1" height="41" width="146" left="500" top="168" visible="true" toolTipText="" exposeToVba="notExposed" isReferenceObject="true" linkSize="true" linkConnections="true" linkAnimations="linkWithExpression" linkBaseObject="(RA-BAS) P_AIn Graphics Library.AIN_AlarmPolygon" linkToolTipText="true" backStyle="transparent" backColor="#E0E0E0" foreColor="black" lineStyle="solid" lineWidth="2" patternStyle="none" patternColor="black" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
    </rectangle>
    <button name="AIN_Button_Faceplate1" height="35" width="140" left="503" top="171" visible="true" toolTipText="" exposeToVba="notExposed" isReferenceObject="true" linkSize="true" linkConnections="true" linkAnimations="linkWithExpression" linkBaseObject="(RA-BAS) P_AIn Graphics Library.AIN_Button_Faceplate" linkToolTipText="true" style="3d" captureCursor="false" highlightOnFocus="true" tabIndex="6">
        <command pressAction="" repeatAction="" releaseAction="Display ($#102.Inf_Lib$) $#102.Inf_Type$-Faceplate /T{#102},{#103},&quot;#120&quot;,&quot;#121&quot;,{X} #120 #121" repeatRate="0.25"/>
        <up patternColor="black" patternStyle="none" backColor="#E0E0E0" backStyle="solid" foreColor="black" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
            <caption fontFamily="Arial" fontSize="8" bold="false" italic="false" underline="false" strikethrough="false" caption=""/>
            <imageSettings imageReference="noImage"/>
        </up>
        <down downSameAsUp="true" patternColor="black" patternStyle="none" backColor="#ECE9D8" backStyle="solid" foreColor="black" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
            <caption fontFamily="Arial" fontSize="8" bold="false" italic="false" underline="false" strikethrough="false" caption=""/>
            <imageSettings imageReference="noImage"/>
        </down>
        <ability showDisabledState="false" expression="" enabledWhenExpressionIsTrue="true" disabledImageType="useGrayscale"/>
        <confirm confirmAction="false" buttonSetting="okCancel" titleBar="true" titleBarText="Confirmation" windowPosition="Centered of screen">
            <caption fontFamily="Arial" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Are you sure you want to perform this action?"/>
            <imageSettings imageReference="noImage"/>
        </confirm>
    </button>
    <button name="AIN_Button_Quick1" height="35" width="140" left="503" top="171" visible="true" toolTipText="" exposeToVba="notExposed" isReferenceObject="true" linkSize="true" linkConnections="true" linkAnimations="linkWithExpression" linkBaseObject="(RA-BAS) P_AIn Graphics Library.AIN_Button_Quick" linkToolTipText="true" style="3d" captureCursor="false" highlightOnFocus="true" tabIndex="5">
        <command pressAction="" repeatAction="" releaseAction="Display ($#102.Inf_Lib$) $#102.Inf_Type$-Quick /T{#102},{#103},&quot;#120&quot;,&quot;#121&quot;,{X} #120 #121" repeatRate="0.25"/>
     </button>
    <parameters>
        <parameter name="#102" description="Object Tag (P_AIn, P_AInAdv, P_AInDual, or P_AInMulti)" value=" "/>
        <parameter name="#103" description="Path (include program scope if tag is a program scope tag)" value="[BOP]"/>
        <parameter name="#120" description="Additional display parameter (e.g. /X100 or /CC) (optional)" value="0"/>
        <parameter name="#121" description="Additional display parameter (e.g. /Y100) (optional)" value="0"/>
        <parameter name="#122" description="0 = Always show Faceplate;  1= Show Quick Display for users without Maintenance Access (Code C); 2 = Always Show Quick Display" value="0"/>
    </parameters>
</group>

您的問題尚不清楚。 是否要為每個生成的節點創建一個輸出xml文件? 還是要將它們全部合並到一個輸出xml文件中? 我認為您想為每個節點創建單獨的xml文件。 將這些xml文件視為文本文件,您只需將字符串“ GO_P_AIn1”替換為“ GO_P_AIn”等即可

這就是我要做的:

Dim n as Integer = 2 'The number to add
Dim objReader As New System.IO.StreamReader(XMLFilePath)
Dim output as System.IO.StreamWriter
Dim outputPath as String = XMLFilePath & n.ToString
Dim xmlContent As String = objReader.ReadToEnd
objReader.Close()
xmlContent.Replace("GO_P_AIn1", "GO_P_AIn" & n) 'Replace the content with desired number
output = My.Computer.FileSystem.OpenTextFileWriter(outputPath)
output.Write(xmlContent)
output.Close()

遍歷您要創建的節點數,這幾乎就是您需要做的

我將使用XML Linq和Regex

Imports System.Xml
Imports System.Xml.Linq
Imports System.Text.RegularExpressions
Module Module1

    Sub Main()
        Dim xml As String = _
                "<?xml version=""1.0"" encoding=""utf-8""?>" & _
                "<root>" & _
                "<group name=""GO_P_AIn1"" visible=""true"" wallpaper=""false"">" & _
                  "<rectangle name=""AIN_AlarmPolygon1"" height=""41"" > </rectangle>" & _
                  "<button name=""AIN_Button_Quick1"" height=""35"" width=""140""> </button>" & _
                "</group>" & _
                "</root>"

        Dim doc As XDocument = XDocument.Parse(xml)
        Dim root As XElement = doc.Descendants("root").FirstOrDefault
        Dim group As XElement = doc.Descendants("group").FirstOrDefault()
        Dim groupString As String = group.ToString()

        For index = 2 To 10
            Dim newGroup As XElement = XElement.Parse(groupString)
            root.Add(newGroup)
            For Each item As XElement In newGroup.DescendantsAndSelf().Where(Function(x) Not x.Attribute("name") Is Nothing)
                Dim name As XAttribute = item.Attribute("name")
                Dim value As String = CType(name, String)
                Dim newValue As String = Regex.Replace(value, "\d+", index.ToString())
                name.Value = newValue
            Next item

        Next index

    End Sub

End Module

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM