Results 1 to 14 of 14

Thread: [RESOLVED] Trying to read multiple lines comments from file

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    12

    Resolved [RESOLVED] Trying to read multiple lines comments from file

    Hello!

    i will try to explain the problem im having in the easiest way.

    i have an XML file which i need to get information from.
    the information is commented, meaning if i need the name it will be between <Name>X</Name>
    which is super easy to do.

    however sometimes i have the comments tag appearing twice in my file and i need to be able to read between all the places where Comments is mentioned:
    lets say this is the file

    <Comments>X</Comments>
    <Comments>Y</Comments>

    i want an output like this:

    Comments: X, Y

    Thank yo so much

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Trying to read multiple lines comments from file

    How are you currently reading the XML file?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    12

    Re: Trying to read multiple lines comments from file

    Using richtextbox

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Trying to read multiple lines comments from file

    The RichTextBox control has nothing at all to do with XML. It is specifically intended to render RTF data. If you wanted to display XML then you could just do so as plain text in a RichTextBox but that would be no help in actually reading the data as XML.

    There are numerous types in the .NET Framework for reading XML so you should do some research on that. It's not something I've done much of so I'm not sure what the current best option is but some options that exist and have done for some time are the XmlDocument and XmlReader.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    12

    Re: Trying to read multiple lines comments from file

    its a 5 line xml which i read as text file. to be honest it doesnt have to be an XML thats just for organization, lets say it was a *.txt file,

    thank you

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Trying to read multiple lines comments from file

    IF it was a text file then you would deal with it completely differently to an XML file. Is the format of this file entirely within your control?

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    12

    Re: Trying to read multiple lines comments from file

    im creating a services monitor, there are many tasks that run and export an 5-7 lines xml

    the services are exported as *.xml but i access them as a textfile with richtextbox

    everything works as intended, however i am facing the problem that when filtering out the X, Y from <Name>X</Name> <Name>Y</Name> i can only access the X and will not able to filter out the Y too.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Trying to read multiple lines comments from file

    If you can do some of it but not the rest then you're doing something wrong. We can't know what's you're doing wrong if we can't see what you're doing. ALWAYS show us the code. It's ALWAYS relevant.

    That said, if the file is XML then you should read it as XML. It's not hard to do so, to be frank, it's just lazy to not even try.

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Trying to read multiple lines comments from file

    Also, they are not comments...they are nodes... that's how XML works... your best best is to use a DOM parser to red the file and parse out the data you want... it's going to be a ton easier than treating it like a text file.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Trying to read multiple lines comments from file

    Just a guess, not at all clear what you are doing. This code uses XElement, XML Literals, and LINQ.

    Code:
            Dim pth As String = "path to xml file here"
    
            Dim xe As XElement
    
            ''if production
            'xe = XElement.Load(pth)
    
            ''else if test use literal
            xe = <root>
                     <!-- this is an XML comment -->
                     <!-- next are two nodes named Comments -->
                     <Comments>U</Comments>
                     <Comments>V</Comments>
                     <!-- next are two nodes named Name -->
                     <Name>X</Name>
                     <Name>Y</Name>
                     <subs>
                         <!-- next are two nodes named Name -->
                         <Name>X1</Name>
                         <Name>Y1</Name>
                     </subs>
                 </root>
    
            Dim ie As IEnumerable(Of XElement)
    
            'get Name nodes at top level
            ie = xe.<Name>
            For Each el As XElement In ie 'examine results
                Stop
            Next
    
            'get Name nodes at ANY level
            ie = xe...<Name>
            For Each el As XElement In ie 'examine results
                Stop
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    12

    Re: Trying to read multiple lines comments from file

    that is not exactly what i am looking for because there may be unkown comments with <Name> that i will need to read dynamically.

    right now i am using:

    Code:
    /*usage/
    Dim rtb as New RichTextBox
    rtb.LoadFile(FileDestinationVariable, RichTextBoxStreamType.PlainText)
    GetStatus(rtb.text)
    /*end usage/
    
        Public Function GetStatus(ByVal x As String)
    
            Dim s As String = x
            Dim i As Integer = s.IndexOf("<Name>")
            Dim f As String = s.Substring(i + "<Name>".Length, s.IndexOf("</Name", i + 1) - i - "</Name".Length)
            f = f.Trim
            Return f
        End Function
    that works, however i get result of only the first <Name> tag and will that not proceed showing me the rest, if they exist

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Trying to read multiple lines comments from file

    Quote Originally Posted by ShAAAMen View Post
    that is not exactly what i am looking for because there may be unkown comments with <Name> that i will need to read dynamically.

    right now i am using:

    Code:
    /*usage/
    Dim rtb as New RichTextBox
    rtb.LoadFile(FileDestinationVariable, RichTextBoxStreamType.PlainText)
    GetStatus(rtb.text)
    /*end usage/
    
        Public Function GetStatus(ByVal x As String)
    
            Dim s As String = x
            Dim i As Integer = s.IndexOf("<Name>")
            Dim f As String = s.Substring(i + "<Name>".Length, s.IndexOf("</Name", i + 1) - i - "</Name".Length)
            f = f.Trim
            Return f
        End Function
    that works, however i get result of only the first <Name> tag and will that not proceed showing me the rest, if they exist
    What do you think these lines do?
    Code:
            Dim ie As IEnumerable(Of XElement)
    
            'get Name nodes at top level
            ie = xe.<Name>
            For Each el As XElement In ie 'examine results
                Stop
            Next
    
            'get Name nodes at ANY level
            ie = xe...<Name>
            For Each el As XElement In ie 'examine results
                Stop
            Next
    The ones in red will get all of the nodes (nodes, please stop calling them comments, they are nodes) ... for Name ... returned as a list... you then LOOP through that list and process each one as you want... The Stop is just there to show that you need to do something with it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    12

    Re: Trying to read multiple lines comments from file

    thank you so much, your code has helped me understand things better ♥

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Trying to read multiple lines comments from file

    Quote Originally Posted by techgnome View Post
    What do you think these lines do?
    Code:
            Dim ie As IEnumerable(Of XElement)
    
            'get Name nodes at top level
            ie = xe.<Name>
            For Each el As XElement In ie 'examine results
                Stop
            Next
    
            'get Name nodes at ANY level
            ie = xe...<Name>
            For Each el As XElement In ie 'examine results
                Stop
            Next
    The ones in red will get all of the nodes (nodes, please stop calling them comments, they are nodes) ... for Name ... returned as a list... you then LOOP through that list and process each one as you want... The Stop is just there to show that you need to do something with it.

    -tg
    Been very busy and just saw that I needed to point some things out, but tg to the rescue. THX!
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width