Solved

CONCATENATE XML INTO ONE FIELD

Posted on 2011-09-30
17
206 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 74

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
 
LVL 74

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 74

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

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 74

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 74

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 74

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 74

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

10 Experts available now in Live!

Get 1:1 Help Now