• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

IS there a simpler way to do this?

Im always looking for that easier or streamline way of doing it. Here is my code ... it works just fine but I keep feeling there simple way to do this or express so that it parses the xml and returns all including the attributes into the RTB


Dim doc = XDocument.Load(myPath & "testXML.xml")

        For Each myCon In doc...<Contact>
            rtb1.AppendText(myCon.@Fname & " " & myCon.@Lname & vbNewLine)
            rtb1.AppendText("Member: " & myCon.@Member & vbNewLine)
            rtb1.AppendText("HomePhone: " & myCon.<HomePhone>.Value & vbNewLine)
            rtb1.AppendText("Cell Phone: " & myCon.<CellPhone>.Value & vbNewLine)
            rtb1.AppendText("Address: " & myCon.<Address>.Value & vbNewLine)
            rtb1.AppendText("City: " & myCon.<City>.Value & vbNewLine)
            rtb1.AppendText("State: " & myCon.<State>.Value & vbNewLine)
            rtb1.AppendText("Notes: " & myCon.<Notes>.Value & vbNewLine & vbNewLine)
        Next

Open in new window

0
vbMarkO
Asked:
vbMarkO
1 Solution
 
jpaulinoCommented:
You could use a StringBuilder() class:


        For Each myCon In doc...<Contact>
            sb.AppendLine(String.Format("{0} {1}", myCon.@Fname, myCon.@Lname))
            sb.AppendLine("Member: " & myCon.@Member)
            ' ...
            sb.AppendLine("Notes: " & myCon.<Notes>.Value)
            sb.AppendLine()
        Next
        rtb1.AppendText(sb.ToString())


but that only helps reading ... not a BIG improvement.
0
 
vbMarkOAuthor Commented:
True ... nearly the same ... there might not be a more streamline way of doing it ... I was just curious ... keep this open a bit to see if anyone else might have some input.

Would it change things if ... I was to take out the text ... "Member:" - "HomePhone:" - "Cell Phone:" and such like and only return and display whats in the xml file? Would that help maybe?
0
 
CodeCruiserCommented:
>only return and display whats in the xml file
Plain xml? Then

IO.File.ReadAllText("path")

would work.

Or you can use another For Each loop within the loop and only use one Append line.



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
x77Commented:
I think you want show Xml Data on a RitchTextBox.

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/5192ddf3-c1c8-4ef8-ad4d-25b80bec8f75/

This Entry proposes a generic method to shows Xml Data and colour it on a RitchTextBox.
0
 
vbMarkOAuthor Commented:

x77 not what i'm after ...
I can get the xml displayed like this

<Contact Fname="John" Lname="Doe" Member="Yes">
  <HomePhone>None Available</HomePhone>
  <CellPhone>731-555-1234</CellPhone>
  <Address>123 Some Street</Address>
  <City>Milan</City>
  <State>TN</State>
  <Notes> A simple description can go here or note</Notes>
</Contact>
<Contact Fname="Jane" Lname="Doe" Member="Yes">
  <HomePhone>731-234-6587</HomePhone>
  <CellPhone>731-555-1234</CellPhone>
  <Address>765 Another Street</Address>
  <City>Milan</City>
  <State>TN</State>
  <Notes> A simple description can go here or note</Notes>
</Contact>
by doing this

        Dim doc = XDocument.Load(myPath & "testXML.xml")
        For Each myCon In doc...<Contact>
            rtb1.AppendText(myCon.ToString() & vbNewLine)
        Next
But my code in first post returns this

John Doe
Member: Yes
HomePhone: None Available
Cell Phone: 731-555-1234
Address: 123 Some Street
City: Milan
State: TN
Notes:  A simple description can go here or note

Jane Doe
Member: Yes
HomePhone: 731-234-6587
Cell Phone: 731-555-1234
Address: 765 Another Street
City: Milan
State: TN
Notes:  A simple description can go here or note

CodeCruiser you have my attention ... you said with a for each and one appendline correct?
I'll give that a go ... see if I can get that to happen ... but feel free to post it ...
0
 
vbMarkOAuthor Commented:
Nope I'm not figuring out how to use one appendline

waqit for example from CodeCruiser or anyone
0
 
CodeCruiserCommented:
Here is the sample code I tried. It may look like same number of lines of code but you wont have to change this code if you change the number of fields in xml


            For Each myCon In doc...<Contact>
                For Each a In myCon.Attributes
                    MsgBox(a.Name.ToString & " : " & a.Value)
                Next
                For Each b In myCon.Descendants
                    MsgBox(b.Name.ToString & " : " & b.Value)
                Next
            Next
0
 
vbMarkOAuthor Commented:
Exactly kind of thing I was looking for .... I will play with this code a lot ... I like it when I am given something I didn't consider ...
 The code works .. I love it

Thank you
0
 
CodeCruiserCommented:
Glad to help :-)
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now