Retrieving value from an XML file
-
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 :-(
-
Not at all a helpful answer, but thanks for taking the time to write something :-(
-
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>:
' loop thro each element in XML doc and replace the reliabiltiy codes
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")I'd like to help you, but I'm a bit unclear as to what you need to do. Do you want to take,
3
and change it to
reliabilityCode
and do the same for
6411, 6821
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:
-
I'd like to help you, but I'm a bit unclear as to what you need to do. Do you want to take,
3
and change it to
reliabilityCode
and do the same for
6411, 6821
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:
Hi David, No. What I want to do is take: 3 and replace it with: 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.
-
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 eachdescripGrp
, so maybe you are accessing it in the wrong way.That is correct. There is in each element just one 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 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.
-
Hi David, No. What I want to do is take: 3 and replace it with: 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 XmlNodexDoc.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")
-
That is correct. There is in each element just one 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 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.
-
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 XmlNodexDoc.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 fromcodeID = xNode.InnerText
instead of your
xNode.InnerText
I replaced the
codeID
withcodeText
(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 :) -
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>:
' loop thro each element in XML doc and replace the reliabiltiy codes
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")You can get and set the value using LINQ to XML and XML axis properties.
Dim conceptor = XDocument.Load("C:\file.xml")
' Retrieve value
Dim subjectField = conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value
' Change value
conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value = "1234"
' Save changes
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. TheValue
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"
-
You can get and set the value using LINQ to XML and XML axis properties.
Dim conceptor = XDocument.Load("C:\file.xml")
' Retrieve value
Dim subjectField = conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value
' Change value
conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value = "1234"
' Save changes
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. TheValue
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.