Untitled
unknown
vbscript
2 years ago
4.6 kB
5
Indexable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AddPlayer("1", "Fred1") RemovePlayer("1", "Fred") SaveTeam("1", "WPH2", "17", "1", "Paul", "Richard", "Garry") End Sub Private Sub SaveTeam(ByVal teamid As String, ByVal teamName As String, ByVal ageGroup As String, ByVal division As String, ByVal coach As String, ByVal teamManager As String, ByVal assistantCoach As String) 'load your XML file Dim seasons = XDocument.Load("Data/Seasons.xml") 'pull a collection of the types of elements you want to query Dim teams = seasons.Root.Elements("Season").Elements("Team") 'Query for the list of XElemets that match the item you are looking for in this case Team id Dim item As IEnumerable(Of XElement) = From e2 In teams Where e2.Attribute("id") = teamid Select e2 Dim output = item(0).ToString() + vbNewLine + vbNewLine 'Loop through item should be only 1 item if you query was unique enough If item.Count = 1 Then 'Set the values of the item(0) XML Elements eg. TeamName item(0).Element("Name").Value = teamName item(0).Element("AgeGroup").Value = ageGroup item(0).Element("Division").Value = division item(0).Element("TeamManager").Value = teamManager item(0).Element("AssistantCoach").Value = assistantCoach item(0).Element("Coach").Value = coach output += item(0).ToString() End If 'Show the part of the XML you are modifying TextBox1.Text = output Dim settings As New XmlWriterSettings() settings.Async = True settings.Indent = True Dim OutputXML As XmlWriter = XmlWriter.Create("Data/Seasons1.xml", settings) 'OutputXML.Flush() seasons.WriteTo(OutputXML) OutputXML.Close() OutputXML.Dispose() End Sub Sub AddPlayer(ByVal teamID As String, ByVal playerID As String) 'load your XML file Dim seasons = XDocument.Load("Data/Seasons.xml") 'pull a collection of the types of elements you want to query Dim teams = seasons.Root.Elements("Season").Elements("Team") 'Query for the list of XElemets that match the item you are looking for in this case Team id Dim item As IEnumerable(Of XElement) = From e2 In teams Where e2.Attribute("id") = "1" Select e2 Dim output = item(0).ToString() + vbNewLine + vbNewLine item(0).Element("Players").Add(New XElement("Player", "", New XAttribute("id", playerID))) output += item(0).ToString() 'Show the part of the XML you are modifying TextBox1.Text = output Dim settings As New XmlWriterSettings() settings.Async = True settings.Indent = True Dim OutputXML As XmlWriter = XmlWriter.Create("Data/Seasons1.xml", settings) 'OutputXML.Flush() seasons.WriteTo(OutputXML) OutputXML.Close() OutputXML.Dispose() End Sub Sub RemovePlayer(ByVal teamID As String, ByVal playerID As String) 'load your XML file Dim seasons = XDocument.Load("Data/Seasons.xml") 'pull a collection of the types of elements you want to query Dim teams = seasons.Root.Elements("Season").Elements("Team") 'Query for the list of XElemets that match the item you are looking for in this case Team id Dim item As IEnumerable(Of XElement) = From e2 In teams Where e2.Attribute("id") = teamID Select e2 Dim output = item.ToString() + vbNewLine + vbNewLine For Each player In item(0).Element("Players").Elements("Player") If player.Attribute("id") = playerID Then player.Remove() End If Next ' item(0).Element("Players").Add(New XElement("Player", "", New XAttribute("id", playerID))) output += item.ToString() 'Show the part of the XML you are modifying TextBox1.Text = output Dim settings As New XmlWriterSettings() settings.Async = True settings.Indent = True Dim OutputXML As XmlWriter = XmlWriter.Create("Data/Seasons1.xml", settings) 'OutputXML.Flush() seasons.WriteTo(OutputXML) OutputXML.Close() OutputXML.Dispose() End Sub
Editor is loading...