Question Looping Through / Writing XML Document

MadDokK

Member
Joined
Apr 25, 2009
Messages
5
Programming Experience
1-3
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
 
Your regex reading of xml (or is it not?) is very weird, one would normally use Xml tools rather than string parsing for handling the hierarcal xml nodes and the element values/attributes. System.Xml tools is also very old fashioned compared to newer System.Xml.Linq tools, in case you want to get updated.
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.
Xml format requires a single root node, add this (start element) before you add all child Account elements. I would prefer closing it explicitly before closing document, but WriteEndDocument will actually close any open elements automatically.
 
Thank you very much. I added a tag to encapsulate everything and it works great.

The reason I use regular expressions to parse the data is because when the program starts it loads the XML file and parses it. It then places the data in an array. To save everything I format it as ID(489120)NAME(Name goes Here)ATTACKPTS(32131) etc. I then parse the data out of it when needed.

What would be a better way to go about it?
 
What would be a better way to go about it?
Maybe you can handle the xml as xml :)
 
Back
Top