如何从xml文件复制完整节点,并通过更改Vb.net中元素的名称来创建多个节点

时间:2016-06-24 06:02:31

标签: xml vb.net xml-parsing

我想从xml文件中复制完整节点并使用该节点创建新的xml文件。

假设该节点由对象组成......如button-1,Poygon-1 ......等 我想通过将名称更改为...... Button-2,Polygon-2 ..等来创建多个节点 就像我的节点名称是
group name =“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>

2 个答案:

答案 0 :(得分:0)

你的问题有些不清楚。 是否要为生成的每个节点创建一个输出xml文件?或者您想将所有这些合并到一个输出xml文件中? 我想你想为每个节点创建单独的xml文件。 将这些xml文件视为文本文件,您只需将字符串“GO_P_AIn1”替换为“GO_P_AIn”&amp;东西

我将这样做:

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()

循环显示您想要创建的节点数量,这几乎就是您需要做的事情

答案 1 :(得分:0)

我会使用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