Solved

CONCATENATE XML INTO ONE FIELD

Posted on 2011-09-30
17
209 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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 350 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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

697 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