?
Solved

VB.net XML output

Posted on 2014-02-12
4
Medium Priority
?
410 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 668 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 668 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 664 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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

771 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