Top DevX Stories
Healthcare Adopts Virtualization, But Slowly
WorkLight Adds BlackBerry Support to its All-in-One Development Platform
Virtualization -- Security Is Still a Concern
iAd for Developers Called "Ineffective"
Choosing the Right Storage for Application Data
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > .NET

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-02-2008, 09:46 AM
Rens.Duijsens's Avatar
Rens.Duijsens Rens.Duijsens is offline
Registered User
 
Join Date: Mar 2008
Location: Blaricum, The Netherlands
Posts: 72
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:
  1. Read in an XML-file
  2. Display this file in a window
  3. Let the user make an X-Path selection (later on, this should be possible by clicking on the data in the display window)
  4. Gray out the not selected data, en highlight the selected data

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?
Reply With Quote
  #2  
Old 03-02-2008, 04:38 PM
Phil Weber Phil Weber is offline
Super Moderator
 
Join Date: Nov 2003
Location: Portland, OR
Posts: 8,320
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.
Attached Files
File Type: zip XPathViewer.zip (15.7 KB, 115 views)
__________________
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.
Reply With Quote
  #3  
Old 03-02-2008, 05:48 PM
Rens.Duijsens's Avatar
Rens.Duijsens Rens.Duijsens is offline
Registered User
 
Join Date: Mar 2008
Location: Blaricum, The Netherlands
Posts: 72
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
Reply With Quote
  #4  
Old 03-03-2008, 12:07 PM
Hack's Avatar
Hack Hack is offline
Super Moderator
 
Join Date: Apr 2007
Location: Sterling Heights, Michigan
Posts: 8,243
Quote:
Originally Posted by Phil Weber
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.
Very nice Phil! :thumbs up:
__________________
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
Reply With Quote
  #5  
Old 03-03-2008, 12:35 PM
Rens.Duijsens's Avatar
Rens.Duijsens Rens.Duijsens is offline
Registered User
 
Join Date: Mar 2008
Location: Blaricum, The Netherlands
Posts: 72
Quote:
Originally Posted by Phil Weber
The hard part is highlighting the selected nodes. Please see the attached project.
And so it is. :-)

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>
Selection: //ELEMENT
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>
But now......
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 texts in NodeNo 7, 8, 9 are not highlighted.

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
Reply With Quote
  #6  
Old 03-03-2008, 01:50 PM
Rens.Duijsens's Avatar
Rens.Duijsens Rens.Duijsens is offline
Registered User
 
Join Date: Mar 2008
Location: Blaricum, The Netherlands
Posts: 72
Quote:
Originally Posted by Rens.Duijsens
I'll figure it out. :-)
Oke....

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
Reply With Quote
  #7  
Old 03-03-2008, 02:25 PM
Rens.Duijsens's Avatar
Rens.Duijsens Rens.Duijsens is offline
Registered User
 
Join Date: Mar 2008
Location: Blaricum, The Netherlands
Posts: 72
Quote:
Originally Posted by Rens.Duijsens
It might be a very simple way for a solution, but it works. :-)
Yep.... It's TO simple a solution. :-)

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.
:-)
Reply With Quote
  #8  
Old 03-03-2008, 02:37 PM
Hack's Avatar
Hack Hack is offline
Super Moderator
 
Join Date: Apr 2007
Location: Sterling Heights, Michigan
Posts: 8,243
Quote:
Originally Posted by Rens.Duijsens
Selections that have single lines a a result work fine, like //ELEMENT, but results that are bigger do not work like //NODE. :-(
What happens on the bigger ones?
__________________
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
Reply With Quote
  #9  
Old 03-03-2008, 02:40 PM
Rens.Duijsens's Avatar
Rens.Duijsens Rens.Duijsens is offline
Registered User
 
Join Date: Mar 2008
Location: Blaricum, The Netherlands
Posts: 72
All stays gray.....

The selected text is not found.
It's missing the CrLf or so.
I'm inverstigating.
Reply With Quote
  #10  
Old 03-03-2008, 02:57 PM
Rens.Duijsens's Avatar
Rens.Duijsens Rens.Duijsens is offline
Registered User
 
Join Date: Mar 2008
Location: Blaricum, The Netherlands
Posts: 72
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!
Reply With Quote
  #11  
Old 03-04-2008, 12:45 AM
Phil Weber Phil Weber is offline
Super Moderator
 
Join Date: Nov 2003
Location: Portland, OR
Posts: 8,320
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
I had not considered multiple matches; my regular expression was only highlighting the first instance of each match.
__________________
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!
Reply With Quote
  #12  
Old 03-04-2008, 03:40 AM
Rens.Duijsens's Avatar
Rens.Duijsens Rens.Duijsens is offline
Registered User
 
Join Date: Mar 2008
Location: Blaricum, The Netherlands
Posts: 72
Thumbs up

Yep!
This works great.

Quote:
Originally Posted by Phil Weber
my regular expression was only highlighting the first instance of each match.
Oke, oke.......
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
Reply With Quote
  #13  
Old 03-04-2008, 09:59 AM
Rens.Duijsens's Avatar
Rens.Duijsens Rens.Duijsens is offline
Registered User
 
Join Date: Mar 2008
Location: Blaricum, The Netherlands
Posts: 72
Quote:
Originally Posted by Rens.Duijsens
I'll be working on my project using this as a basis.
I have a question.

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
Reply With Quote
  #14  
Old 03-04-2008, 11:06 AM
Hack's Avatar
Hack Hack is offline
Super Moderator
 
Join Date: Apr 2007
Location: Sterling Heights, Michigan
Posts: 8,243
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
Reply With Quote
  #15  
Old 03-04-2008, 12:19 PM
Phil Weber Phil Weber is offline
Super Moderator
 
Join Date: Nov 2003
Location: Portland, OR
Posts: 8,320
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!
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

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


All times are GMT -4. The time now is 02:33 AM.


Sponsored Links



Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.