【问题标题】:How to copy complete node from xml file and create multiple node by changing names of elements in Vb.net如何从 xml 文件复制完整节点并通过更改 Vb.net 中元素的名称来创建多个节点
【发布时间】:2016-06-24 06:02:31
【问题描述】:

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

假设该节点由对象组成...例如 button-1、Poygon-1...等
我想通过将名称更改为 ....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>

【问题讨论】:

  • 您需要发布一个更好的示例来显示更改后的数据。不要包含 up/down/ability/confirm 等子节点,除非它们很重要。
  • 谢谢,我的要求是……我想生成一个包含多个节点的 xmi 文件。假设...输入 xml 节点: 我想要这样的输出: n 次

标签: xml vb.net xml-parsing


【解决方案1】:

您的问题不清楚。 您想为每个生成的节点创建一个输出 xml 文件吗?还是要将它们全部合并到一个输出 xml 文件中? 我认为您想为每个节点创建单独的 xml 文件。 将这些xml文件视为文本文件,您只需将字符串“GO_P_AIn1”替换为“GO_P_AIn”&something

我会这样做:

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

循环遍历您要创建的节点数,这几乎就是您需要做的事情

【讨论】:

  • 谢谢,我的要求是……我想生成一个包含节点数的xmi文件。
  • 谢谢,我的要求是……我想生成一个包含多个节点的 xmi 文件。假设...输入 xml 节点: 我想要这样的输出: n 次
  • 然后为您想要更改的所有字符串添加更多替换。我想没有别的办法了。
【解决方案2】:

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

【讨论】:

  • 谢谢它的工作......但是先生如何每次都在新的xml文件中附加该节点?并保存新的 xml 文件。
  • 使用 group.WriteXml(filename) 保存结果。我不知道“附加在新的 xml 文件中”是什么意思。你能提供更多细节吗?
  • 在每个索引之后,我们都会获得新节点 ....like GO_P_AIn1, GO_P_AIn2, GO_P_AIn3...等......所以我每次都追加新节点。
  • 输入来自哪里?你如何确定新的索引是什么?我不能只是随机地向对象添加信息。
  • 实际上我会提供一个excel文件作为输入......它包含索引和所有其他数据。假设我的索引是 10 .....那么我必须从 1-10 生成 10 个节点。这些节点将保存在新的 xml 文件中....最后我需要包含“n”个节点的 xml 文件。
猜你喜欢
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 2013-04-13
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
相关资源
最近更新 更多