Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

XML Query Assistance

xml query syntax

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1 Whitey

Whitey

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 259 posts

Posted 10 March 2014 - 08:58 PM


 

Im use to PHP and doing a query seems similar but i need help with syntax.

 

I have an XML file that looks like this


<?xml version="1.0" standalone="yes"?>
  <switchIP>
    <SwitchName>ES1UA1</SwitchName>
    <IP>205.71.10.4</IP>
    <Location>02-157-1-q</Location>
  </switchIP>
  <switchIP>
    <SwitchName>ES2UA1</SwitchName>
    <IP>205.71.10.5</IP>
    <Location>02-157-1-q</Location>
  </switchIP>
</DataSet1>
and i am trying to do a query where it will search for "ES2UA1" and i get the data "IP" and "Location"
 
I know i am some where in the ball park with
Dim doc = XDocument.Load(My.Application.Info.DirectoryPath & ".\xmlData.xml")

Dim marker2 = From x In doc...<switchIP> _
                  Where x.SwitchName = "ES1UA1" _
                  Select x
for each x as y
msgbox(y.IP)

Edited by Whitey, 10 March 2014 - 08:59 PM.


#2 zeroGiven

zeroGiven

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 61 posts

Posted 11 March 2014 - 04:42 AM

Personally, I would just use the .ReadXML method of the Dataset to put this back into a dataset making it easy to access the information.

Dim ds as New Dataset
ds.ReadXML("The path to the file that holds this xml script")
DataGridView1.DataSource = ds.Tables(0)

of course, I just set a datasource for a datagridvew to show the simplicity of reading this into a dataset.

 

From there you could use some LINQ

Dim Query = From item in ds.Tables(0).AsEnumerable() Select item

Dim IPs = Query.Where(Function(p) p.Field(Of String)("SwitchName") = "ES1UA1")

For Each itm in IPs
   MsgBox (itm.Field(Of String)("IP"))
Next


#3 zeroGiven

zeroGiven

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 61 posts

Posted 11 March 2014 - 05:42 AM

OK, I also looked in to using your method and found something interesting.

 

Using this code against your xml file

        Dim doc As XElement = XElement.Load("D:\zzDS.xml")
        Dim stCnt = From address In doc.Elements("switchIP")
        Where CStr(address.Element("SwitchName")) = "ES1UA1"
        Select address

        For Each item In stCnt
            MsgBox(item.Element("IP"))
        Next

produced 0 results.

Until i removed the link in the header of the xml.

 

Try changing line 2 to just

<DataSet1> instead of <DataSet1 xmlns="http://tempuri.org/DataSet1.xsd">



#4 Whitey

Whitey

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 259 posts

Posted 11 March 2014 - 08:24 AM

Where are tutorials on this so I can read and understand it haha I appreciate your help!

#5 zeroGiven

zeroGiven

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 61 posts

Posted 11 March 2014 - 09:30 AM

This has a lot of info.



#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 11 March 2014 - 09:35 AM

I think you should take a look to xml selectNode methods, here is a tutorial about it



#7 Whitey

Whitey

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 259 posts

Posted 11 March 2014 - 05:36 PM


  <switchIP>
    <SwitchName>ES1UA1</SwitchName>
    <IP>205.71.10.4</IP>
    <Location>02-157-1-q</Location>
  </switchIP>
</DataSet1>
 

         Dim switchname As String = "ES1UA1"
         Dim xelement As XElement = xelement.Load(My.Application.Info.DirectoryPath & "\xmlData.xml")
         Dim query = _
               From q In xelement.Elements("switchIP") _
               Where CStr(q.Element("SwitchName")) = switchname _
               Select q
         For Each xEle As XElement In query
             MsgBox(xEle.Element("IP").Value & " - " & xEle.Element("Location").Value)
         Next xEle

 

It did work when i removed xmlns="http://tempuri.org/DataSet1.xsd">

 

but the visual basic creates that xml dataset for me.. so how do i get it to not add that in there?


But i cant remove that, then my dataset wont work to add more columns and tables to it from the application


Edited by Whitey, 11 March 2014 - 05:25 PM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download