Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CONCATENATE XML INTO ONE FIELD

Posted on 2011-09-30
17
Medium Priority
?
216 Views
Last Modified: 2012-05-12
I ahve a dropdownlist

the following xml..
<document>
- <row>
  <TCODE>865755</TCODE>
  <NAME>Marcus</NAME>
  <CITY>Chicago</CITY>
 </row>

Id like to display as

Marcus,Chicago in teh dropdownlist . how is that done?
0
Comment
Question by:zachvaldez
[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
  • 8
  • 7
  • 2
17 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36894017
Where does this XML come from? A file? A web service call? A string?
0
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 36894050
var t = xmlDocument.SelectSingleNode("/document/row/NAME").InnerText + "," + xmlDocument.SelectSingleNode("/document/row/CITY").InnerText;

I think something like that.
0
 

Author Comment

by:zachvaldez
ID: 36894315
A file which I added to the project
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36895342
If you have .NET 3.5+, you could use LINQ to achieve this:

Public Class Form1

    Private options As IEnumerable(Of String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim xdoc As XDocument = XDocument.Load("input.xml")
        Dim str As String

        Me.options = From row In xdoc.<document>.Elements() _
                     Select __Entry__ = (From result In row.Elements() _
                                     Select result.Value).Aggregate(Function(seed, iterator) String.Concat(seed, ",", iterator))

        Me.ComboBox1.DataSource = Me.options.ToList()
        Me.ComboBox1.DisplayMember = "__Entry__"

    End Sub
End Class

Open in new window

0
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 36901974
Did my code?

var t = xmlDocument.SelectSingleNode("/document/row/NAME").InnerText + "," + xmlDocument.SelectSingleNode("/document/row/CITY").InnerText;
0
 

Author Comment

by:zachvaldez
ID: 36903814
I'm using vb.net.Like what I mentioned,I have an XML file but I only want to use 2 row elements out of 4 and they are name and city. I would like to display the result in a drpdownlist control and id like to find out steps and code to do that
I'm learning aspnet/xml. Thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36903957
So are you saying that none of the code provided thus far has been helpful? If so, can you clarify your confusion?
0
 

Author Comment

by:zachvaldez
ID: 36904433
I havent work with LINQ , Just regular Sql queries Can you convert your code without use of LINQ?
thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36905558
Is this a Web project or a Forms project?
0
 

Author Comment

by:zachvaldez
ID: 36906033
Web
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36909704
You could do something like this:
Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim xdoc As New System.Xml.XmlDocument()
        Dim items As New List(Of String)

        xdoc.Load(Server.MapPath("input.xml"))

        For Each row As System.Xml.XmlNode In xdoc.SelectNodes("//row")
            Dim sb As New System.Text.StringBuilder()

            For Each child As System.Xml.XmlNode In row.ChildNodes
                If child.Name <> "TCODE" Then
                    sb.AppendFormat("{0},", child.InnerText)
                End If
            Next

            sb.Length -= 1  ' Remove trailing comma

            items.Add(sb.ToString())
        Next

        Me.DropDownList1.DataSource = items
        Me.DropDownList1.DataBind()
    End Sub

End Class

Open in new window

0
 

Author Comment

by:zachvaldez
ID: 36914755
here's a better xml structure

      <row><TCODE>77777</TCODE>
            <NAME>MJK</NAME>
            <ADDRESS_1>888 Western Ave.</ADDRESS_1>
            <CITY>Sunter</CITY>
            <STATE>OH</STATE>
            <ZIP_CODE>73773-8888</ZIP_CODE>
            <VCODE>VIII</VCODE>
            <DCODE>G</DCODE>
      </row>

              What Id like to display is NAME AND CITY NODES IN THE DROPDOWNLIST
     
       Also can you show me how to display single node values also. for example if I just want to show
State only. and hide the rest as well as concatenate 2 nodes like Code + Vcode..
Sorry I have a lot of questions...

thanks that was truly helpful
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1400 total points
ID: 36914804
What Id like to display is NAME AND CITY NODES IN THE DROPDOWNLIST
You can change lines 10 - 2 to:

        For Each row As System.Xml.XmlNode In xdoc.SelectNodes("//row")
            Dim item As String = row.SelectSingleNode("NAME").InnerText

            item = String.Concat(item, ",", row.SelectSingleNode("CITY").InnerText)

            items.Add(item)
        Next

Open in new window


Also can you show me how to display single node values also. for example if I just want to show State only.
For example:
Me.Label1.Text = xdoc.SelectSingleNode("/document/row/STATE").InnerText

Open in new window


as well as concatenate 2 nodes like Code + Vcode.
See the first code snippet of this post, lines 2 & 4.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36914807
Make sure you understand how XPath works. It will be required for selecting nodes in an XmlDocument.
0
 

Author Comment

by:zachvaldez
ID: 36914929
Thanks for the bonus!
0
 

Author Closing Comment

by:zachvaldez
ID: 36914943
Superb!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36914976
NP. Glad to help  = )
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

604 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