Solved

VB.net XML output

Posted on 2014-02-12
4
402 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
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 62

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now