-
Aug 2nd, 2020, 12:44 PM
#1
Thread Starter
New Member
[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
-
Aug 2nd, 2020, 04:16 PM
#2
Re: Trying to read multiple lines comments from file
How are you currently reading the XML file?
-
Aug 2nd, 2020, 06:27 PM
#3
Thread Starter
New Member
Re: Trying to read multiple lines comments from file
-
Aug 2nd, 2020, 11:26 PM
#4
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.
-
Aug 3rd, 2020, 04:45 AM
#5
Thread Starter
New Member
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
-
Aug 3rd, 2020, 04:56 AM
#6
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?
-
Aug 3rd, 2020, 05:00 AM
#7
Thread Starter
New Member
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.
-
Aug 3rd, 2020, 06:34 AM
#8
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.
-
Aug 3rd, 2020, 01:06 PM
#9
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
-
Aug 3rd, 2020, 01:25 PM
#10
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
-
Aug 3rd, 2020, 04:10 PM
#11
Thread Starter
New Member
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
-
Aug 3rd, 2020, 04:45 PM
#12
Re: Trying to read multiple lines comments from file
Originally Posted by ShAAAMen
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
-
Aug 4th, 2020, 09:53 AM
#13
Thread Starter
New Member
Re: Trying to read multiple lines comments from file
thank you so much, your code has helped me understand things better ♥
-
Aug 4th, 2020, 03:25 PM
#14
Re: Trying to read multiple lines comments from file
Originally Posted by techgnome
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|