Solved

CONCATENATE XML INTO ONE FIELD

Posted on 2011-09-30
17
207 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

9 Experts available now in Live!

Get 1:1 Help Now