1,105,352 Community Members

Write and sort xml

Member Avatar
Lowchernhwee
Newbie Poster
17 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Well i have troubles sorting the xml whenever i created a new info so here are the codes

Imports System.Xml
Imports System.IO
Imports System.Text
Public Class Form1
    Dim m_strdata As String(,) = New String(999, 4) {}
    Dim w As Integer = 0
    Private Sub read()

        Dim m_xmlr As XmlTextReader
        'Create the XML Reader
        If File.Exists("1.xml") Then
            m_xmlr = New XmlTextReader("1.xml")
            'Disable whitespace so that you don't have to read over whitespaces
            m_xmlr.WhitespaceHandling = WhitespaceHandling.None
            'read the xml declaration and advance to family tag
            m_xmlr.Read()
            'read the family tag
            m_xmlr.Read()
            'Load the Loop
            While Not m_xmlr.EOF
                'Go to the name tag
                m_xmlr.Read()
                'if not start element exit while loop
                If Not m_xmlr.IsStartElement() Then
                    Exit While
                End If
                m_xmlr.Read()
                'Get the firstName Element Value
                'Get the lastName Element Value
                m_strdata(w, 0) = m_xmlr.ReadElementString("Day")
                w = w + 1
                'Write Result to the Console
            End While
            'close the reader
            m_xmlr.Close()
        End If
    End Sub
    Private Sub write()
        Dim Writer As XmlTextWriter = New XmlTextWriter("1.xml", Encoding.UTF8)
        Dim Filename As String = "1.xml"
        Dim DOMDocument As XmlDocument = New XmlDocument
        Dim write As String
        Dim converter As Integer
        Dim number As Integer = 0
        Dim trigger As Integer = 0
        Dim RootElement As XmlElement
        Dim Element As XmlElement
        Dim TextVideo As XmlText
        converter = Convert.ToInt64(TextBox1.Text)
        If (converter < Convert.ToInt64(m_strdata(0, 0))) Then
            write = converter.ToString
            trigger = 1
        Else
            write = m_strdata(0, 0)
        End If
        Writer.WriteStartDocument()

        Writer.WriteStartElement("Events")

        Writer.WriteStartElement("students")
        Writer.WriteElementString("Day", write)


        Writer.WriteEndElement()
        'add tags / elements
        'add tags / elements
        Writer.WriteEndElement()

        Writer.WriteEndDocument()
        'everything written here are kept override and replce
        Writer.Flush()

        Writer.Close()

        While w <> 0 'total events is the same
            w = w - 1
            If (trigger = 0 AndAlso converter < Convert.ToInt64(m_strdata(number + 1, 0))) Then
                write = converter.ToString
                trigger = 1
                DOMDocument.Load(Filename)

                RootElement = DOMDocument.DocumentElement
                Element = DOMDocument.CreateElement("students")
                RootElement.AppendChild(Element)

                RootElement = DOMDocument.DocumentElement

                Element = DOMDocument.CreateElement("Day")
                TextVideo = _
                DOMDocument.CreateTextNode(write)
                RootElement.LastChild.AppendChild(Element)
                RootElement.LastChild.LastChild.AppendChild(TextVideo)
                DOMDocument.Save(Filename)
                number = number + 1
            End If
            DOMDocument.Load(Filename)

            RootElement = DOMDocument.DocumentElement
            Element = DOMDocument.CreateElement("students")
            RootElement.AppendChild(Element)

            RootElement = DOMDocument.DocumentElement

            Element = DOMDocument.CreateElement("Day")
            TextVideo = _
            DOMDocument.CreateTextNode(m_strdata(number, 0))
            RootElement.LastChild.AppendChild(Element)
            RootElement.LastChild.LastChild.AppendChild(TextVideo)
            DOMDocument.Save(Filename)
            number = number + 1
        End While
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Filename As String = "1.xml"
        Dim DOMDocument As XmlDocument = New XmlDocument

        If File.Exists(Filename) Then
            read()
            My.Computer.FileSystem.DeleteFile("1.xml")
            write()
            '"Append" to next info the previous 1 need something....
        Else
            Dim Writer As XmlTextWriter = New XmlTextWriter("1.xml", Encoding.UTF8)

            Writer.WriteStartDocument()

            Writer.WriteStartElement("Events")

            Writer.WriteStartElement("students")
            Writer.WriteElementString("Day", TextBox1.Text)


            Writer.WriteEndElement()
            'add tags / elements
            'add tags / elements
            Writer.WriteEndElement()

            Writer.WriteEndDocument()
            'everything written here are kept override and replce
            Writer.Flush()

            Writer.Close()
        End If
    End Sub
End Class

The results if i create 4, 2, 3, 1 are

<?xml version="1.0" encoding="UTF-8"?>
-<Events> 
    -<students>
    <Day>1</Day> 
    </students> 
    -<students>
    <Day>2</Day> 
    </students>
    -<students> 
    <Day>3</Day>
    </students> 
    -<students>
    <Day>4</Day> 
    </students>
    -<students> 
    <Day> </Day> 
    </students> 
</Events>

The results if i create 4, 2, 3, 5 are

<?xml version="1.0" encoding="UTF-8"?>
-<Events> 
    -<students> 
    <Day>2</Day> 
    </students>
    -<students>
    <Day>2</Day>
    </students> 
    -<students> 
    <Day>3</Day>
    </students>
    -<students>
    <Day>4</Day>
    </students>
    -<students>
    <Day>4</Day>
    </students> 
</Events>

so any1 can help me please?

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

This is .Net code. I have asked that this post be moved there.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

Please show a sample of your original xml file. Then manually create and show a sample of how it should look after it has been sorted.

Member Avatar
Lowchernhwee
Newbie Poster
17 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

well ok
initally

<?xml version="1.0" encoding="utf-8"?>
<Events>
  <students>
    <Day>1</Day>
  </students>
  <students>
    <Day>2</Day>
  </students>
</Events>

if i add 3

<?xml version="1.0" encoding="utf-8"?>
<Events>
  <students>
    <Day>1</Day>
  </students>
  <students>
    <Day>1</Day>
  </students>
  <students>
    <Day>2</Day>
  </students>
</Events>

hmm when i search , there are better codes to use for swapping yet it doesnt work for me

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
1
 

I'm not sure this what you are attempting, but I have assumed:

  1. you need to read an xml source file
  2. you need to add a new "students" element
  3. you want to save the file with "students" sorted on "Day"

      Dim doc As New Xml.XmlDocument ' create a XMLDocument to hold the file
      doc.Load("SourceFile.xml")     ' load the file
    
      ' Since Events is your main element it is the "DocumentElement"
      Dim Events As Xml.XmlNode = doc.DocumentElement
    
      ' add a new "students" element to "Events"
         Dim elStudents As Xml.XmlElement = doc.CreateElement("students")
         Dim elDay As Xml.XmlElement = doc.CreateElement("Day")
         elDay.InnerText = "3"
         elStudents.AppendChild(elDay)
         Events.AppendChild(elStudents)
    
      ' since there are only "students" elements as child nodes of "Events", 
      ' all child nodes are "students"
    
      ' we will create a list that can be sorted and load it from "Events.ChildNodes"
    
      Dim sortedstudents As List(Of Xml.XmlElement) = _
         (From el In Events.ChildNodes Select CType(el, Xml.XmlElement)).ToList
    
      ' I am assuming that "Day" is an Integer;
      ' therefore, I am converting it to an Integer for comparison during sorting
    
      sortedstudents.Sort(Function(el1 As Xml.XmlElement, el2 As Xml.XmlElement) _
                           CInt(el1.SelectSingleNode("Day").InnerText).CompareTo( _
                           CInt(el2.SelectSingleNode("Day").InnerText)))
    
      Events.RemoveAll() ' Delete all exiting child nodes from Events
    
      ' now append the sorted "students" elements to the empty "Events" element
    
      For Each el As Xml.XmlElement In sortedstudents
         Events.AppendChild(el)
      Next
    
      ' save the processed file
      doc.Save("fred.xml")
    
Question Answered as of 1 Year Ago by TnTinMN and AndreRet
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article