copy xml node from one file to another

gixxerrider73

New member
Joined
Nov 18, 2012
Messages
4
Programming Experience
Beginner
I've search all over for a good example of this and cant get any to do what I need. I just don't understand all of the xml stuff well enough to get this to work, so thanks for helping. This seems to be pretty simple.

I'm trying to copy and xmlnode from one file into a new file. I have to use .net 3.5 since I use VS2008.

My xml file has a list of games with an attribute called Genre. I'm try to copy only games with a specific genre to a file.

The list of genres is in my DGV_GenreList. So for each Genre create a new xml file with only games of that genre. I'm able to print to the console the games for each genre, I'm just having an issue with the copy the xml node.

HERE IS A SMALL PORTION OF MY XML
VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<menu>
    <game name="10-Yard Fight (USA, Europe)" index="true" image="1">
    <description>10-Yard Fight (USA, Europe)</description>
    <cloneof>
    </cloneof>
    <crc>3D564757</crc>
    <manufacturer>Nintendo</manufacturer>
    <year>1985</year>[COLOR=#FF0000]
    <genre>Football Games</genre>[/COLOR]
    <rating>HSRS - GA (General Audience)</rating>
    <enabled>Yes</enabled>
  </game>
  <game name="1942 (Japan, USA)" index="" image="">
    <description>1942 (Japan, USA)</description>
    <cloneof>
    </cloneof>
    <crc>171251E3</crc>
    <manufacturer>Capcom</manufacturer>
    <year>1986</year>[COLOR=#FF0000]
    <genre>Shooter Games</genre>[/COLOR]
    <rating>HSRS - GA (General Audience)</rating>
    <enabled>Yes</enabled>
  </game>
  <game name="1943 - The Battle of Midway (USA)" index="" image="">
    <description>1943 - The Battle of Midway (USA)</description>
    <cloneof>
    </cloneof>
    <crc>12C6D5C7</crc>
    <manufacturer>Capcom</manufacturer>
    <year>1988</year>
    <genre>Shooter Games</genre>
    <rating>HSRS - GA (General Audience)</rating>
    <enabled>Yes</enabled>
  </game>
  
</menu>


this is my function
VB.NET:
Sub CreateGenreMenus()
 
        Dim GenreCnt As Integer
        Dim SysDatabase, GenreTitle, GenreEnabled As String
        Dim ROMListCnt, ROMCnt, NodeCnt As Integer
        SysDatabase = "Gamelist.xml"
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        'Create the XML Document
        m_xmld = New XmlDocument()
        'Load the Xml file
        m_xmld.Load(SysDatabase)
        'Get the list of name nodes 
        m_nodelist = m_xmld.SelectNodes("/menu/game")
        'Debug.Print("Game count: " & m_nodelist.Count)
        ROMListCnt = m_nodelist.Count
        'Set Progress Bar
        ProgressForm.ItemProgressBar.Maximum = ROMListCnt
        '------ Create New xml file
        Dim doc = New XDocument()
        Dim Menu = <Menu>
                   </Menu>
        doc.Add(Menu)
         '------ end new xml file

        Try
            'now create a database for each genre
            'create new xml file
            
            ' loop through genre list in Datagridview
            For a As Integer = 0 To Me.DGV_GenreList.Rows.Count - 1
                If DGV_GenreList.Rows(a).Cells(1).Value = True Then
                    
                    For Each m_node In m_nodelist
                        
                        If (m_node.ChildNodes.Item(5).InnerText = DGV_GenreList.Rows(a).Cells("GenreName").Value) Then
                            Console.Write("GAME NAME: " & m_node.Attributes.GetNamedItem("name").Value & vbCrLf)
       [B][COLOR=#FF0000]'THIS IS WHERE I WANT TO COPY THE ENTIRE XMLNODE FROM ONE FILE TO ANOTHER.[/COLOR][/B]

                        End If
                    Next
                End If
                
                'save xml
            Next a 'goto next item in genre list
            'Save xml file
            doc.Save("Genremenu.xml")
        Catch errorVariable As Exception
            'Error trapping
            MsgBox(errorVariable.ToString())
        End Try
        
    End Sub

Thanks for looking!
Jay
 
Last edited by a moderator:
Linq and xml literal expressions makes this kind of operation real easy, see this example:
        Dim doc = XDocument.Load("games.xml")

        Dim groups = From x In doc...<game> Group By x.<genre>.Value Into Group

        For Each g In groups
            Dim newdoc = <menu><%= g.Group %></menu>
            newdoc.Save(g.genre & ".xml")
        Next

Example extracting a specific genre:
        Dim games = From x In doc...<game> Where x.<genre>.Value = "Shooter Games"
        Dim newdoc = <menu><%= games %></menu>
        newdoc.Save("genre.xml")
 
JohnH,

WOW! I don't know what to say but THANK YOU. I've been stuck on this for days. I'm not a true programmer but I try really hard to figure these things out for myself. That worked perfectly!!

You just saved me hours of endless searching.

Jay
 

Latest posts

Back
Top