?
Solved

CONCATENATE XML INTO ONE FIELD

Posted on 2011-09-30
17
Medium Priority
?
213 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
Technology Partners: 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!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

777 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