Share via

Parse xml without linq

OSVBNET 1,401 Reputation points
2022-06-24T07:42:01.77+00:00

Hello,
Need to parse a single field of an xml document (without LINQ if possible)
I even tried to open it as string:
Dim fileReader As String = My.Computer.FileSystem.ReadAllText("e:\file.xml")

All I need is to extract the data inside "Field_Name" tag:

<Field_Name type="SZ">Data to Extract</Field_Name>
You guys know any safe method to do so?
Thanks :)

Developer technologies | VB

2 answers

Sort by: Most helpful
  1. Castorix31 91,876 Reputation points
    2022-06-24T15:40:31.62+00:00

    You can also use Microsoft XML

     'Add reference To "Microsoft XML v6.0"   
    

    At beginning

    Imports MSXML2   
    

    Test with XML file

            Dim dom As DOMDocument60 = New DOMDocument60()  
            Dim bRet As Boolean = dom.load("E:\Test.xml")  
            If bRet Then  
                Dim nodes As IXMLDOMNodeList = dom.childNodes  
                For i = 0 To nodes.length - 1  
                    Dim node As IXMLDOMNode = nodes(i)  
                    ProcessNode(node)  
                Next  
            End If  
    

    Utility function, cannot post here (to be improved, I just did a quick test with 2 XML files...) : ProcessNode

    Was this answer helpful?

    0 comments No comments

  2. Viorel 127K Reputation points
    2022-06-24T08:15:04.597+00:00

    If you hate LINQ, then see XPath:

    Imports System.Xml.XPath  
    . . .  
    Dim xdoc = XDocument.Load("e:\file.xml")  
    Dim extracted_data As String = xdoc.XPathSelectElement("//Field_Name[@type='SZ']").Value  
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.