Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

LINQ to XML

Hello,

I have a file and xml file that I am reading using a dataset, I want to change that to read it with LINQ.

The XML file is:

<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
  <user>
    <uid>5026934506</uid>
    <name>Dummy User</name>
  </user>
</fql_query_response>

Can anyone help with the code to read the XML in LINQ? I am coding in VB.
0
DanielJay
Asked:
DanielJay
  • 9
  • 4
  • 2
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
what exactly ur trying to read?
u can use XElement to load the xml file and parse:
XElement root = XElement.Load("yourFile.xml");
0
 
Meir RivkinFull stack Software EngineerCommented:
in vb.net:
dim root as XElement = XElement.Load("yourFile.xml")
0
 
DanielJayAuthor Commented:
I am trying to read the UID and Name parts.
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
Meir RivkinFull stack Software EngineerCommented:
the way your xml is built makes it impossible to parse it without "hacking" the xml content.
the first node contains 2 xml namespaces and an attreibute which the xml parser don't know how to handle.
is the xml creation done by 3rd party app ro by your?
0
 
Meir RivkinFull stack Software EngineerCommented:
is the xml creation done by 3rd party app or by yourself?
0
 
DanielJayAuthor Commented:
It's Facebook XML, and I have used it with XMLTextReader to pass it into a dataset.
0
 
Meir RivkinFull stack Software EngineerCommented:
anyway, u can use this code snippet which removes the nsamespaces and returns the user id and name:

Sub Main()
        Dim root As XElement = RemoveAllNamespaces(XElement.Load("c:\temp\1.xml"))
        Dim uid As String = root.XPathSelectElement("user/uid").Value
        Dim name As String = root.XPathSelectElement("user/name").Value

    End Sub
  

    'Core recursion function 
    Private Function RemoveAllNamespaces(ByVal xmlDocument As XElement) As XElement
        If Not xmlDocument.HasElements Then
            Dim xElement As New XElement(xmlDocument.Name.LocalName)
            xElement.Value = xmlDocument.Value
            Return xElement
        End If
        Return New XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().[Select](Function(el) RemoveAllNamespaces(el)))
    End Function

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
for the code above u need to add:
Imports System.Xml.XPath



-> if this xml may contain multiple users and u wish to get a list of them (Id and Name):

<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
  <user>
    <uid>5026934506</uid>
    <name>Dummy User</name>
  </user>
  <user>
    <uid>123456789</uid>
    <name>Ziggy</name>
  </user>
  <user>
    <uid>1029384756</uid>
    <name>Robert</name>
  </user>
</fql_query_response>

use this code snippet:
Sub Main()

        Dim root As XElement = RemoveAllNamespaces(XElement.Load("c:\temp\1.xml"))
        Dim users = root.Elements("user").[Select](Function(n) New With {.ID = n.Element("uid").Value, .Name = n.Element("name").Value})

        For Each item In users
            Dim id As String = item.ID
            Dim name As String = item.Name
        Next


    End Sub


    'Core recursion function 
    Private Function RemoveAllNamespaces(ByVal xmlDocument As XElement) As XElement
        If Not xmlDocument.HasElements Then
            Dim xElement As New XElement(xmlDocument.Name.LocalName)
            xElement.Value = xmlDocument.Value
            Return xElement
        End If
        Return New XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().[Select](Function(el) RemoveAllNamespaces(el)))
    End Function

End Module

Open in new window

0
 
DanielJayAuthor Commented:
Thanks, I can do this already, I am looking for help with LINQ to XML.
0
 
Meir RivkinFull stack Software EngineerCommented:
what kind of help, what do u need?
0
 
DanielJayAuthor Commented:
I want to read the XML file using LINQ.
0
 
Meir RivkinFull stack Software EngineerCommented:
the code i've posted is linq (system.xml.linq).
what exactly do u mean by linq?
0
 
Meir RivkinFull stack Software EngineerCommented:
0
 
Fernando SotoRetiredCommented:
Hi DanielJay;

This should give you what you want.

Fernando
Imports System.Xml.Linq
Imports <xmlns:fb="http://api.facebook.com/1.0/">

' Open the XML document
Dim ele As XElement = XElement.Load("XMLData.xml")
' Create a namespace object
Dim ns As XNamespace = GetXmlNamespace(fb)

' Get the needed info
Dim uid As String = ele.Descendants(ns + "uid").Value
Dim name As String = ele.Descendants(ns + "name").Value

MessageBox.Show("UID = " & uid & " : Name = " & name)

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi DanielJay;

Has the issues been resolved?

Fernando
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now