Home > Software engineering >  Append list of strings to XML file in VB.NET
Append list of strings to XML file in VB.NET

Time:05-04

I need a little bit of help for simplify a work and don't do it manually, if possible.

I have a list of strings:

<title>TITLE 1</title>
<title>TITLE 2</title>
<title>TITLE 3</title>
<title>TITLE 4</title>
<title>TITLE 5</title>
ETC...

There are over 1200 strings, just to let you know. I also have an XML file with the same numbers of elements, each one in this format:

<game id="N">
    <url>URL</url>
    <img>IMG_URL</img>
    <date>DATE</date>
    <genre>GENRE</genre>
    <publisher>PUBLISHER</publisher>
    <developer>DEVELOPER</developer>
    <platforms>PLATFORMS</platforms>
    <vote>VOTE</vote>
    <review>REVIEW_URL</review>
    <info><![CDATA[INFORMATIONS]]></info>
</game>

What I need to do is appending for each of the <title> strings between each </img> and before <date>. It's possible to do it with VB.NET (I can't code in C#/C ) and, if yes, could someone please help me writing the code? Or maybe there is a simpler way with a regex, etc... Thanks!

CodePudding user response:

Using XElement and LINQ.

    Dim URI As String = "your path here"
    Dim fileXE As XElement
    'fileXE = XElement.Load(URI) ' - load XML,  uncomment in production

    'for testing = remove in production
    Dim titls As New List(Of String)
    titls.AddRange({"<title>TITLE 1</title>",
                    "<title>TITLE 2</title>",
                    "<title>TITLE 3</title>",
                    "<title>TITLE 4</title>",
                    "<title>TITLE 5</title>"})

    'for testing = remove in production
    fileXE = <games>
                 <game id="A">
                     <url>URL</url>
                     <img>IMG_URL</img>
                     <date>DATE</date>
                     <genre>GENRE</genre>
                     <publisher>PUBLISHER</publisher>
                     <developer>DEVELOPER</developer>
                     <platforms>PLATFORMS</platforms>
                     <vote>VOTE</vote>
                     <review>REVIEW_URL</review>
                     <info><![CDATA[INFORMATIONS]]></info>
                 </game>
                 <game id="B">
                     <url>URL</url>
                     <img>IMG_URL</img>
                     <date>DATE</date>
                     <genre>GENRE</genre>
                     <publisher>PUBLISHER</publisher>
                     <developer>DEVELOPER</developer>
                     <platforms>PLATFORMS</platforms>
                     <vote>VOTE</vote>
                     <review>REVIEW_URL</review>
                     <info><![CDATA[INFORMATIONS]]></info>
                 </game>
                 <game id="C">
                     <url>URL</url>
                     <img>IMG_URL</img>
                     <date>DATE</date>
                     <genre>GENRE</genre>
                     <publisher>PUBLISHER</publisher>
                     <developer>DEVELOPER</developer>
                     <platforms>PLATFORMS</platforms>
                     <vote>VOTE</vote>
                     <review>REVIEW_URL</review>
                     <info><![CDATA[INFORMATIONS]]></info>
                 </game>
             </games>
    'end for testing

    'get all img nodes
    Dim imgs As IEnumerable(Of XElement) = fileXE...<img>

    For Each el As XElement In imgs 'add one string per img
        Dim xe As XElement
        Try
            xe = XElement.Parse(titls(0)) 'get a title
            titls.RemoveAt(0) 'remove it
            el.AddAfterSelf(xe) 'add it
        Catch ex As Exception
            Stop
        End Try
    Next
    fileXE.Save(URI) 'save file
    Stop
  • Related