|
#1
|
||||
|
||||
|
XML-file --> Read --> Display selection
Hello,
I have been programming in VB6 for al long time. Last week I installed Vistual Studio 2008. (I hoped that would be easier.... But it is not, or is it just me?) I'm trying to develop a programm that does the following:
I have been fighting with textReader and with XPathDocument as well as XPathNavigator but I'm still not able to let them do what I need. Can somebody please advise me on the best way to tackle this please? |
|
#2
|
|||
|
|||
|
It actually is easier, once you learn the .NET Framework. Selecting XML nodes based on an XPath expression is easy. The hard part is highlighting the selected nodes. Please see the attached project.
__________________
Phil Weber http://www.philweber.com Please post questions to the forums, where others may benefit. I do not offer free assistance by e-mail. Thank you! Last edited by Phil Weber; 03-02-2008 at 04:53 PM. |
|
#3
|
||||
|
||||
|
WOW!
This is GREAT! This is exactly what I need for a basis. I'll build further from this. You made my life a LOT easier. Great big bombastic thanky. (Jar Jar Binx) Thanks Rens |
|
#4
|
||||
|
||||
|
Quote:
__________________
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section. ![]() Please use [Code]your code goes in here[/Code] tags when posting code. Before posting your question, did you look here? Got a question on Linux? Visit our Linux sister site. Modifications Required For VB6 Apps To Work On Vista ![]() Good Article: Language Enhancements In Visual Basic 2010 Upgrading VB6 Code To VB.NET Microsoft MVP 2005/2006/2007/2008/2009/2010 |
|
#5
|
||||
|
||||
|
Quote:
I've tried the following XML testfile: Code:
<?xml version="1.0"?>
<ROOT NodeNo="1" pattern="test">
<NODE NodeNo="2">
<ELEMENT NodeNo="3">Element 1</ELEMENT>
<ELEMENT NodeNo="4">Element 2</ELEMENT>
<ELEMENT NodeNo="5">Element 3</ELEMENT>
</NODE>
<NODE NodeNo="6">
<ELEMENT NodeNo="7">Element 1</ELEMENT>
<ELEMENT NodeNo="8">Element 2</ELEMENT>
<ELEMENT NodeNo="9">Element 3</ELEMENT>
</NODE>
</ROOT>
and all works fine. Code:
<?xml version="1.0"?>
<ROOT NodeNo="1" pattern="test">
<NODE NodeNo="2">
<ELEMENT NodeNo="3">Element 1</ELEMENT>
<ELEMENT NodeNo="4">Element 2</ELEMENT>
<ELEMENT NodeNo="5">Element 3</ELEMENT>
</NODE>
<NODE NodeNo="6">
<ELEMENT NodeNo="7">Element 1</ELEMENT>
<ELEMENT NodeNo="8">Element 2</ELEMENT>
<ELEMENT NodeNo="9">Element 3</ELEMENT>
</NODE>
</ROOT>
Select: //ELEMENT/text() And we see: Code:
<?xml version="1.0"?>
<ROOT NodeNo="1" pattern="test">
<NODE NodeNo="2">
<ELEMENT NodeNo="3">Element 1</ELEMENT>
<ELEMENT NodeNo="4">Element 2</ELEMENT>
<ELEMENT NodeNo="5">Element 3</ELEMENT>
</NODE>
<NODE NodeNo="6">
<ELEMENT NodeNo="7">Element 1</ELEMENT>
<ELEMENT NodeNo="8">Element 2</ELEMENT>
<ELEMENT NodeNo="9">Element 3</ELEMENT>
</NODE>
</ROOT>
The regular expression just finds the first instance. And so would //ELEMENT fail if you would leave out the attributes. Than only the first set of ELEMENT's will be highlighted. Same reason. :-) I'll figure it out. :-) (Did I mention, I' very happy with the example code?) Rens |
|
#6
|
||||
|
||||
|
Quote:
It might be a very simple way for a solution, but it works. :-) The code for HighlightSelectedNodes() looks like this now. Code:
Private Sub HighlightSelectedNodes(ByVal Nodes As XmlNodeList)
' Clear previous selection
With RichTextBox1
.SelectAll()
.SelectionColor = Color.Gray
.SelectionFont = New Font(.Font, FontStyle.Regular)
End With
If Nodes.Count > 0 Then
For Each Node As XmlNode In Nodes
Debug.WriteLine(Node.OuterXml)
' Q&D Solution (Rens)
Dim num_Index As Integer = InStr(1, RichTextBox1.Text, Node.OuterXml)
While num_Index > 0
With RichTextBox1
.SelectionStart = num_Index - 1
.SelectionLength = Len(Node.OuterXml)
.SelectionColor = Color.Black
.SelectionFont = New Font(.Font, FontStyle.Bold)
End With
num_Index = InStr(num_Index + 1, RichTextBox1.Text, Node.OuterXml)
End While
Next
End If
End Sub
|
|
#7
|
||||
|
||||
|
Quote:
Selections that have single lines a a result work fine, like //ELEMENT, but results that are bigger do not work like //NODE. :-( Back to the drawing board. :-) |
|
#8
|
||||
|
||||
|
Quote:
__________________
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section. ![]() Please use [Code]your code goes in here[/Code] tags when posting code. Before posting your question, did you look here? Got a question on Linux? Visit our Linux sister site. Modifications Required For VB6 Apps To Work On Vista ![]() Good Article: Language Enhancements In Visual Basic 2010 Upgrading VB6 Code To VB.NET Microsoft MVP 2005/2006/2007/2008/2009/2010 |
|
#9
|
||||
|
||||
|
All stays gray.....
The selected text is not found. It's missing the CrLf or so. I'm inverstigating. |
|
#10
|
||||
|
||||
|
The difference is, the text in the RichTextBox is indented, and the nodes resturned bij xpath are not.
The box contains: "<?xml version="1.0"?> <ROOT pattern="test"> <NODE> <ELEMENT>Element 1</ELEMENT> <ELEMENT>Element 2</ELEMENT> <ELEMENT>Element 3</ELEMENT> </NODE> <NODE> <ELEMENT>Element 1</ELEMENT> <ELEMENT>Element 2</ELEMENT> <ELEMENT>Element 3</ELEMENT> </NODE> </ROOT> " Mark the TAB's and CrLf's. The nodes returned by Node.OuterXml contains: "<NODE><ELEMENT>Element 1</ELEMENT><ELEMENT>Element 2</ELEMENT><ELEMENT>Element 3</ELEMENT></NODE>" No TAB's and/or CrLf's. So the compare says: No match! |
|
#11
|
|||
|
|||
|
Sorry, try this:
Code:
Private Sub HighlightSelectedNodes(ByVal Nodes As XmlNodeList)
' Clear previous selection
With RichTextBox1
.SelectAll()
.SelectionColor = Color.Gray
.SelectionFont = New Font(.Font, FontStyle.Regular)
End With
If Nodes.Count > 0 Then
For Each Node As XmlNode In Nodes
Debug.WriteLine(Node.OuterXml)
Dim Matches As MatchCollection
Dim Elements() As String = Split(Node.OuterXml, "><")
If UBound(Elements) > 0 Then
Dim OpenTag As String = Elements(0) & ">"
Dim CloseTag As String = "<" & Elements(UBound(Elements))
Matches = Regex.Matches(RichTextBox1.Text, OpenTag & "(.+?)" & CloseTag, RegexOptions.Singleline)
Else
Matches = Regex.Matches(RichTextBox1.Text, Node.OuterXml, RegexOptions.Singleline)
End If
For Each M As Match In Matches
With RichTextBox1
.SelectionStart = M.Index
.SelectionLength = M.Length
.SelectionColor = Color.Black
.SelectionFont = New Font(.Font, FontStyle.Bold)
End With
Next
Next
End If
End Sub
__________________
Phil Weber http://www.philweber.com Please post questions to the forums, where others may benefit. I do not offer free assistance by e-mail. Thank you! |
|
#12
|
||||
|
||||
|
Yep!
This works great. Quote:
I read the message between the lines. I really should brush up on my knowledge on regular expressions. ![]() Thank you very much. I'll be working on my project using this as a basis. FYI I'm building a help-tool for the creation of X-Path statements for StreamServe events. Friendly greetings Rens |
|
#13
|
||||
|
||||
|
Quote:
I've added some buttons and textboxes. lblPattern btnPattern txtPattern lblMatch btnMatch txtMatch lblValue btnValue txtValue and i have altered the code accordingly. Now, however. No mather in wich textbox I enter an X-path statement, the moment I press <enter> the code btnMatch_Click is executed. How come? In VB6 there was a button property that could make a button react to enter or to escape. I can not find this here. Please advice. Is it possible to let <enter> activate the button that is assigned to the textbox? (And is so, how do I do that please?) Friendly grreetings Rens |
|
#14
|
||||
|
||||
|
Look at the Forms Propertys
Look for an AcceptButton Property Is btnMatch in this property?
__________________
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section. ![]() Please use [Code]your code goes in here[/Code] tags when posting code. Before posting your question, did you look here? Got a question on Linux? Visit our Linux sister site. Modifications Required For VB6 Apps To Work On Vista ![]() Good Article: Language Enhancements In Visual Basic 2010 Upgrading VB6 Code To VB.NET Microsoft MVP 2005/2006/2007/2008/2009/2010 |
|
#15
|
|||
|
|||
|
As Hack points out, if you want to dynamically change which button responds to the Enter key, you can either change the form's AcceptButton property as each textbox gets the focus, or you can handle the Enter key in each textbox's KeyDown event.
__________________
Phil Weber http://www.philweber.com Please post questions to the forums, where others may benefit. I do not offer free assistance by e-mail. Thank you! |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to include XML in XML file? | calgarychinese | XML | 11 | 08-22-2006 07:02 PM |
| wav file fade in out help | jase_dukerider | C++ | 2 | 04-14-2005 07:48 PM |
| How to read objects from file in a loop | mail2vinaybabu | Java | 10 | 02-27-2005 01:07 PM |
| SQL SERVER -> XML File | Vick | Database | 0 | 01-13-2003 08:38 AM |
| XML and XSL transforming | grothlander | XML | 0 | 12-03-2001 10:06 AM |