[英]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},"#120","#121",{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},"#120","#121",{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.