Solved

XML String

Posted on 2008-10-19
8
254 Views
Last Modified: 2012-05-05
Just seeing if someone has a better method out there. I am currently receiving an xml string back in my web app.  I was looking for a way to convert that xml string into something more usable that i can parse through on the fly and go through each one of the elements.

I am using vb.net

Thanks
0
Comment
Question by:jimseiwert
  • 4
  • 4
8 Comments
 
LVL 1

Accepted Solution

by:
ozoid earned 500 total points
ID: 22754326
Hi
i use a simple xmldocument object and a select case method for each node:
You can then read the attributes and innertext from each node and it doesn't matter what order they are in in the xml file.


Function ReadXMLFile(xmlIn as string) as string     

            Dim xd As New XmlDocument()

            Dim outstr As String = ""

            xd.Load(xmlIn)

            Dim xnl As XmlNodeList

            Dim xrn As XmlNode

            xrn = xd.DocumentElement ' root node

            If xrn.HasChildNodes Then

                xnl = xrn.ChildNodes

                For l As Integer = 0 To  xnl.Count - 1

                  Select Case xnl.Name

                    Case "node1"

                      outstr &= xrn.InnerText

                    Case "node2"

                      outstr &= xrn.innerText

                  End Select

                Next

            End if

Return outstr

End Function

Open in new window

0
 
LVL 2

Author Comment

by:jimseiwert
ID: 22754367
Is there a way to do this without passing a file? I am trying to avoid creating a file just to read it and then delete it. I have the xml string already in memory?

Also can you provide an example of how you read your above example.

Thanks,
Jim
0
 
LVL 2

Author Comment

by:jimseiwert
ID: 22754375
I dont need the example, I understand your method. Thanks,

If you can think of a way i can do this without loading a file.
0
 
LVL 1

Assisted Solution

by:ozoid
ozoid earned 500 total points
ID: 22754416
Hi

Use
xd.LoadXml(xmlIn) where xmlIn is a string from memory
or
xd.Load(xmlIn) to load from a file where xmlin is the filename

The code goes something like this:
Create a new empty XMLDocument obkect = xd
Create an empty string for output = outstr
load the string/file into the document object (must be valid XML)
create an empty list for the nodes to go in
create a node object to store the infomation
open the root element (xml files should always have a root element)
if the root element has child elements then go through each one
If the node name = "node1" then append to the output variable outstr the value contained in the xml node
If the node name = "node2" then do the same..
    <rootnode>
      <node1 colour="yellow" >bananas</node1>
      <node1 colour="green" >apples</node1>
      <node2 shape="oblong" >box</node2>
</rootnode>

output: bananasboxapples

not the greatest example i admit - but i hope you get the idea..
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 2

Author Comment

by:jimseiwert
ID: 22754469
I am getting the following message when it gets to the .load - Illegal characters in path.  , any thoughts ?

 I tried to pass it a file and a xml string
0
 
LVL 1

Expert Comment

by:ozoid
ID: 22754495
Have you got a properly formatted xml document
you may need to add
<?xml version="1.0" encoding="utf-8"?>
to the beginning of the string to ensure the XMLDocument object knows whats happening..

the LoadXml function should take a string - exactly what would be the contents of the file..

0
 
LVL 2

Author Comment

by:jimseiwert
ID: 22754586
Hey i was able to read the xml, it was my error. I did not change it to loadxml.

Any idea using your method how to read subtags?
0
 
LVL 1

Expert Comment

by:ozoid
ID: 22756023
Hi
To read child tags - check the node hasChildNodes then use another for next loop and more select case statements.

     If xrn.HasChildNodes Then
                xnl = xrn.ChildNodes
                For l As Integer = 0 To  xnl.Count - 1
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

22 Experts available now in Live!

Get 1:1 Help Now