VB.NET:
Public Sub WriteXML()
Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.ConformanceLevel = ConformanceLevel.Auto
Dim XmlWrt As XmlWriter = XmlWriter.Create("MyXML.xml", settings)
XmlWrt.WriteStartDocument()
For i = 0 To (XMLList.Length - 1) 'Cycle through every name that was processed from the original XML Document
Dim pattern As String = "(?<=ID\()\d+(?=\))" 'Use REGEX to parse out all of the account details.
Dim ID As String = Regex.Match(XMLList(i).ToString, pattern).ToString
pattern = "(?<=NAME\().+(?=\)ATTACK)"
Dim Name As String = Regex.Match(XMLList(i).ToString, pattern).ToString
pattern = "(?<=ATTACKPTS\().+(?=\)RAIDPTS)"
Dim AttackPoints As String = Regex.Match(XMLList(i).ToString, pattern).ToString
pattern = "(?<=RAIDPTS\().+(?=\)MISCPTS)"
Dim RaidPoints As String = Regex.Match(XMLList(i).ToString, pattern).ToString
pattern = "(?<=MISCPTS\().+(?=\)TOTALPTS)"
Dim MiscPoints As String = Regex.Match(XMLList(i).ToString, pattern).ToString
pattern = "(?<=TOTALPTS\().+(?=\)RETIRED)"
Dim TotalPoints As String = Regex.Match(XMLList(i).ToString, pattern).ToString
pattern = "(?<=RETIRED\().+(?=\))"
Dim Retired As String = Regex.Match(XMLList(i).ToString, pattern).ToString
With XmlWrt
.WriteStartElement("ACCOUNT")
.WriteAttributeString("ID", ID)
.WriteAttributeString("RETIRED", Retired)
.WriteElementString("NAME", Name)
.WriteElementString("ATTACKPTS", AttackPoints)
.WriteElementString("RAIDPTS", RaidPoints)
.WriteElementString("MISCPTS", MiscPoints)
.WriteElementString("TOTALPTS", TotalPoints)
.WriteEndElement()
End With
Next
XmlWrt.WriteEndDocument()
XmlWrt.Close()
End Sub
Hello. My program initially reads an XML document. It is then to made adjustments and re-write the XML document. I am having issues on the writing part. When it reads the XML document it places each account into an array. So array XMLList(0) has the name and all the different pts. ID and Retired are attributes of the Account tag which also get stored onto that array in a very simple text format. I then loop through every item of that array parsing the data I need out and re-placing it into the new XML file.
The problem is I end up with a bunch of ACCOUNT tags that don't get closed out until the end instead of each account being it's own item they all get closed in the end. In order to fix this I added the last .WriteEndElement at the end befor the End With. Unfortunately once I do this when I run the code it highlights the first ".WriteStartElement("ACCOUNT")" and says "Token StartElement in state EndRootElement would result in an invalid XML document..." It suggests setting the conformancelevel setting which I did and I still get the error.
Every example of code makes me think that this is the right way but it just won't work. Any help is greatly appreciated.
Thank you in advance!
Brendan