|
I'm new to working with XML in VB.NET and would like a little help after tearing my hair out for a couple of days.
I have an XML file with the following structure:
-<conceptGrp>
-<descripGrp>
<descrip type="subjectField">6411, 6821</descrip>
</descripGrp>
-<languageGrp>
<language lang="DE" type="German"/>
-<termGrp>
<term>Scheren</term>
-<descripGrp>
<descrip type="termType">fullForm</descrip>
</descripGrp>
-<descripGrp>
<descrip type="reliabilityCode">3</descrip>
</descripGrp>
</termGrp>
</languageGrp>
-<languageGrp>
<language lang="EN" type="English"/>
-<termGrp>
<term>scissors</term>
-<descripGrp>
<descrip type="termType">fullForm</descrip>
</descripGrp>
-<descripGrp>
<descrip type="reliabilityCode">3</descrip>
</descripGrp>
</termGrp>
</languageGrp>
</conceptGrp>
First I need to cycle through all the elements in the file (>550000) and for each element extract the text
"6411, 6821" from
<descrip type="subjectField">6411, 6821</descrip>
and
"3" from
<descrip type="reliabilityCode">3</descrip>
The <descrpGrp> appears just once for each element.
The <languageGrp> appears at least twice but up to 20 times.
The <termGrp> appears mostly once but up to 10 times for each <languageGrp>.
The <descrpGrp> appears twice for each <termGrp>.
The <descrip type="reliabilityCode" ...> appears just once for each term.
Using the extracted strings I look them up and need to replace them with text in the element.
This is my code so far for getting the <descrip type="reliabilityCode">3</descrip>:
For Each conceptGrp In xDoc.Elements("conceptGrp")
For Each languageGrp In conceptGrp.Elements("languageGrp")
For Each termGrp In languageGrp.Elements("termGrp")
Dim code = termGrp.Element("reliabilityCode")
For Each descripGP In termGrp.Elements("descripGrp")
For Each descrip In descripGP.Elements("descrip")
Dim reliability As String = xDoc.selectSingleNode("/Categories/category[@descrip='reliability code']").InnerText
Next descrip
Next descripGP
Next termGrp
Next languageGrp
Next conceptGrp
When I look in the locals window I can see "descrip" and for the first node the property "next node" with the value I want but I just can't grab it.
How can I easily get the value and then replace it in the element with my looked up text?
Thanks
Ben
modified 17-May-17 20:17pm.
|
|
|
|
|
If Only You Knew The Power Of The Dark Side XPath!
|
|
|
|
|
Not at all a helpful answer, but thanks for taking the time to write something 
|
|
|
|
|
The helpful bit was the suggestion to use XPath. If you look at the structure of the XML there is only ever one descrip element in each descripGrp , so maybe you are accessing it in the wrong way.
|
|
|
|
|
That is correct.
There is in each element just one <descrip type="subjectField">6411, 6821. There are over 700 possible values for subjectField and there can be multiple values each separated by a comma. Her we have two values "6411" and "6821".
In each element there are a minimum of two languageGrp and a maximum of thirty. It varies throughout the file.
In each languageGrp there is at least one termGrp up to a maximum of 10.
In each termGrp there is just one <descrip type="reliabilityCode">3. There are up to five possible values of this code, here it is "3".
I've never used XPath and have not much useful reference material at the moment. It's all new to me, I'm an old VB/C# horse.
|
|
|
|
|
Ben Senior wrote: not much useful reference material Seriously? There is no end of it, from MSDN documentation to articles in both VB and C#.
|
|
|
|
|
I'd like to help you, but I'm a bit unclear as to what you need to do.
Do you want to take,
<descrip type="reliabilityCode">3</descrip>
and change it to
<descrip type="reliabilityCode">reliabilityCode</descrip>
and do the same for
<descrip type="subjectField">6411, 6821</descrip>
I think this can be done cleaner with xPath statements, rather then lots of looping.
If my understanding is correct, then I will help with the xPath statements.
![Java | [Coffee]](https://codeproject.global.ssl.fastly.net/script/Forums/Images/coffee.gif)
|
|
|
|
|
Hi David,
No. What I want to do is take:
<descrip type="reliabilityCode">3
and replace it with:
<descrip type="reliabilityCode">Very reliable
The "3" can vary and when I get this numerical code I have a function from where I can retrieve its text equivalent. I then need to insert the text equivalent back into the XML file. There are five of these numeric codes.
I'm new to XML and have never used XPath before so I'm struggling at the moment. I've been researching XPath and LINQ and just can't seem to get the hang of them.
I have been using the looping as that's what I'm used to doing in VB and C#.
The last thing that I will need to do is go through each element and depending upon the value of the subject field write the whole element to a new file for that subject. But first thing first and replace the numeric codes with text.
The subjectField is very similar other than there are over 700 subjects. I already have a function which converts the numeric subject field codes into text, and as above I need to insert the text back into the XML file.
|
|
|
|
|
See if this code helps. I realize that it doesn't do exactly what you want, but it shows how xPath works.
Dim xDoc As New XmlDocument
Dim xNodeList As XmlNodeList
Dim xNode As XmlNode
xDoc.Load("C:\Temp\test.xml")
xNodeList = xDoc.SelectNodes("/conceptGrp/descripGrp/descrip[@type='subjectField']")
For Each xNode In xNodeList
xNode.InnerText = xNode.Attributes("type").Value
Debug.Print(xNode.InnerText)
Next
xDoc.Save("C:\Temp\test_1.xml")
|
|
|
|
|
I had to add the root to the code ie. "/mtf/conceptGrp/descripGrp/descrip[@type='subjectField']" and a variable codeID to hold the value from
codeID = xNode.InnerText instead of your
xNode.InnerText
I replaced the codeID with codeText (the corresponding text from my function) and reinserted it into the xml file.
I need to test out the subjectField, but I don't anticipate any difficulties because it basically the same as for reliabilityCode.
Works a treat. Thanks for your help. Now I'm getting there with XPath 
|
|
|
|
|
You can get and set the value using LINQ to XML and XML axis properties.
Dim conceptor = XDocument.Load("C:\file.xml")
Dim subjectField = conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value
conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value = "1234"
conceptor.Save("C:\file.xml")
The descendant axis property, ...<> , gets all descendants that have the name specified within the angle brackets. The attribute axis property, .@ , returns a reference to the value of the specified attribute. The Value axis property returns a reference to the value of the first element in a collection of elements.
"As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
|
|
|
|
|
Thanks for that Meschack,
As I said above I have solved that problem, with the help of people here, and now moved on to the next stage in the project, which is moving along quite well.
I have noted your suggestions along with the others, for reference and future use.
|
|
|
|
|
I have been handed an assignment: update a legacy "window" application. The UI must work the same to avoid user re-training costs. The source code is lost/unavailable. (I suspect it was written by a consultant who did not leave the source code. ) This legacy code will not run on Win7 or Win10. (Now you see why it must be updated immediately.. )
My problem is that the original author "caught" the Shift, Control and Alt keys to select which data frame to display. If none of the three are pressed and held, the most common data is displayed. If the Shift key is pressed and held, the demographics data is displayed. You get the picture. None of these data frames have any interactive controls. The KeyUp, KeyPress, and KeyDown events are not being caught when these keys are pressed:
Private Sub ucDisplay_KeyUp(sender As Object, _
e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Stop
End Sub
Any suggestions on how to catch these events? Can someone point me to a useful reference?
Thank you.
__________________
Lord, grant me the serenity to accept that there are some things I just can’t keep up with, the determination to keep up with the things I must keep up with, and the wisdom to find a good RSS feed from someone who keeps up with what I’d like to, but just don’t have the damn bandwidth to handle right now.
© 2009, Rex Hammock
|
|
|
|
|
Private Shiftdown As Boolean = False
Private altdown As Boolean = False
Private cntrldown As Boolean = False
Private Sub ucDisplay_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case Keys.ShiftKey
Shiftdown = False
Case Keys.Menu
altdown = False
Case Keys.ControlKey
cntrldown = False
End Select
End Sub
Private Sub ucDisplay_Keydown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.ShiftKey
Shiftdown = True
Case Keys.Menu
altdown = True
Case Keys.ControlKey
cntrldown = True
End Select
If Shiftdown AndAlso altdown AndAlso cntrldown Then
End If
End Sub
modified 17-May-17 5:24am.
|
|
|
|
|
The KeyUp event only fires when the key is released. It sounds like you want to handle the KeyDown event instead.
Jalapeno Bob wrote: The source code is lost/unavailable.
Is it a .NET application? If so, and if the consultant didn't use an obfuscator, you might be able to use a decompiler to see what the code is doing. It won't give you a perfect copy of the original source, but it might help.
Free .NET decompiler :: JetBrains dotPeek[^]
JustDecompile .NET Assembly Decompiler & Browser - Telerik[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Great Idea!!! Except: Visual Studio shows Intel 80x86 assembly language, not CLR Intermediate language. No symbol table. Just one .exe file. Last compiled in 2001, probably for Win98. Like I said "Legacy code"
__________________
Lord, grant me the serenity to accept that there are some things I just can’t keep up with, the determination to keep up with the things I must keep up with, and the wisdom to find a good RSS feed from someone who keeps up with what I’d like to, but just don’t have the damn bandwidth to handle right now.
© 2009, Rex Hammock
|
|
|
|
|
GOOD DAY FRIENDS! Please, is it possible to detect when the pc or laptop position changes using visual basic? Take for instance, if pc was initially located at position A and later moved to position B; then, the application should be able to get the position's changes.
In a simplified term, assuming pc was in Lat. 30 and Log. 40, if position changes, then the lat. and log. values should also change.
Finally found what I want but it is in javascript.
Can anyone convert this to a desktop application in vb.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(18.9300, 72.8200),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var latlngbounds = new google.maps.LatLngBounds();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
google.maps.event.addListener(map, 'click', function (e) {
alert("Latitude: " + e.latLng.lat() + "\r\nLongitude: " + e.latLng.lng());
});
}
</script>
<div id="dvMap" style="width: 500px; height: 500px">
</div>
</body>
</html>
Thanks in advance!
modified 12-May-17 15:12pm.
|
|
|
|
|
If the machine has inbuilt GPS hardware then yes, probably. If not, you may be able to get a rough location from wifi connection data. Accuracy will be poor though.
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
Thanks for your response. How can I achieve this your suggestion please?
|
|
|
|
|
Investigate the Google Maps API
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
Thank you so much for your time of response. Please, I tried reading about google's map api but not getting the flow. It only shows the country as a whole. But what am looking for is, assuming a pc position was lat 30.001 and log. 40.687 and was later shifted from initial position to another. I want the software to update it self with the pc current position. Please help. Once again, thanks.
modified 4-Jul-17 1:26am.
|
|
|
|
|
And how is that software supposed to know when the PC has been moved, or to figure out exactly where it is on the surface of the earth? As has already been mentioned, unless the PC contains a GPS receiver then you cannot do it.
|
|
|
|
|
The script posted by me connects to Google Map API and retrieves someone current lat. and log. but it is written in JavaScript.Looking for a way to convert it to VB (VB 6.0 or VB.Net). Any help please.
|
|
|
|
|
OK, but Google Maps API does't magically know with any REAL accuracy where the machine is UNLESS IT HAS A GPS RECEIVER. Without that YOU have to tell Google where the machine is. If you rely on any other detection machanism, like Geomapping an IP, it can be off by dozens of miles. Right now, if I don't tell Google explicitly, it'll guess that my machine is 36 miles from where it really is.
|
|
|
|
|
Thanks for your vital information!
|
|
|
|