Solved

VB.net XML output

Posted on 2014-02-12
4
407 Views
Last Modified: 2014-02-13
The below visual basic Sub ProcessPatientVolume()  successfully creates this XML output:

    <Volume>
      <FacNo>183</FacNo>
      <YearMonth>201312</YearMonth>
      <Visits>1</Visits>
      <FacNo>183</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>1</Visits>
      <FacNo>203</FacNo>
      <YearMonth>201311</YearMonth>
      <Visits>1</Visits>
      <FacNo>203</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>1</Visits>
    </Volume>

I would like the output to include <Item></Item> as shown below:
    <Volume>
      <Item>
      <FacNo>183</FacNo>
      <YearMonth>201312</YearMonth>
      <Visits>341</Visits>
      </Item>
      <Item>
      <FacNo>183</FacNo>
      <YearMonth>201401</YearMonth>
      <Visits>431</Visits>
      </Item>
    </Volume>

How can I add <Item></Item> to the XML output?

   Private Sub ProcessPatientVolume()
dim xVolume As New XElement("Volume")

          Dim SQL As String = ""
        SQL = "SELECT FacNo, SUBSTRING(TimeIn, 1, 6) AS YearMonth, COUNT(VisitID) AS Visits FROM Visit GROUP BY FacNo, SUBSTRING(TimeIn, 1, 6) ORDER BY FacNo, YearMonth"
        Try
            Using CNN As New SqlConnection(CSCLI)
                CNN.Open()
                Using CMD As New SqlCommand(SQL, CNN)
                    Using DR As SqlDataReader = CMD.ExecuteReader
                        While DR.Read
                            xVolume.Add(New XElement("FacNo", DR.Item("FacNo").ToString))
                            xVolume.Add(New XElement("YearMonth", DR.Item("YearMonth").ToString))
                            xVolume.Add(New XElement("Visits", DR.Item("Visits").ToString))
                        End While
                    End Using
                End Using
            End Using

        Catch ex As Exception
            LOG(ex.ToString)
            ex = Nothing
        End Try

    End Sub
0
Comment
Question by:dastaub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 167 total points
ID: 39855562
Change the 3 lines in your While loop to:
                            Dim xItem As New XElement("Item")
                            xItem.Add(New XElement("FacNo", DR.Item("FacNo").ToString))
                            xItem.Add(New XElement("YearMonth", DR.Item("YearMonth").ToString))
                            xItem.Add(New XElement("Visits", DR.Item("Visits").ToString))
                            xVolume.Add(xItem)

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 167 total points
ID: 39855566
While DR.Read
                            Dim xItem As New XElement("Item")
                            xVolume.Add(xItem)
                            xItem .Add(New XElement("FacNo", DR.Item("FacNo").ToString))
                            xItem .Add(New XElement("YearMonth", DR.Item("YearMonth").ToString))
                            xItem .Add(New XElement("Visits", DR.Item("Visits").ToString))
                        End While
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 166 total points
ID: 39856593
Hi dastaub;

Replace your While with this version and the results should be what you are looking for.
   
While DR.Read
    Dim item As New XElement("Item", _
        New XElement("FacNo", DR.Item("FacNo").ToString), _
        New XElement("YearMonth", DR.Item("YearMonth").ToString), _
        New XElement("Visits", DR.Item("Visits").ToString))
    xVolume.Add(item)
End While
0
 

Author Closing Comment

by:dastaub
ID: 39857133
500 points could not be divided equally between 3 answers.  I assume as Computer Science people you would already know that, but I wanted to mention it.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Format Transaction Number 19 48
CSS question 16 62
WPF and Powershell button hover change text 5 61
Suppress if value zero or NULL in crystal report 2 41
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question