How to read number of records from XML file
-
Tuesday, March 27, 2012 1:04 AM
Hello all.
I'm working with XML for the first time and I'm not able to read the number of records from an existing files. As of now I have my records this way:
-<DataNodes> -<Data> <numOne>25</numOne> <StrOperator>+</StrOperator> <numTwo>5</numTwo> <StrEqual>=</StrEqual> <result>30</result> </Data> -<Data> <numOne>23</numOne> <StrOperator>+</StrOperator> <numTwo>3</numTwo> <StrEqual>=</StrEqual> <result>26</result> </Data>Meaning that above I have 2 records, on which I'd would like to append and save back to the same file. But I'd also like to set a maximum to 10 records. I already have it where I can save on the top of each other. So at this point what I need is to create a function to read the number of records already on file and create a loop to get the count so I can trap it and display a message if 10 records have already been saved.
I have a similar function for Access down below:
Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Acme\MathOp.accdb" Dim myConnection As New OleDbConnection(ConnString) Dim mySelectQuery As String = "SELECT COUNT(*) FROM MathOp" Dim myCommand As New OleDbCommand(mySelectQuery, myConnection) myConnection.Open() 'finds the number of records' Dim lngCount As Long lngCount = CLng(myCommand.ExecuteScalar()) 'This is the command that I would like to find out in XML' 'test condition to find out if database has 10 records' If lngCount > 9 Then 'if true, displays messagebox' Dim result As DialogResult = MessageBox.Show("You have reached your maximum allowance of 10 records." & vbNewLine & "If you hit OK this will DELETE all records.", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)Not quite sure how to do it in XML.
Appreciate all the inputs.
<datanodes></datanodes>
All Replies
-
Tuesday, March 27, 2012 1:28 AM
-
Tuesday, March 27, 2012 1:51 AM
Wow Mike, that was so simple. Yet so effective.
Thank you so much.
If not to bother you so much. How would I retrieve those same records to display in a listbox in a same row. For instance like this:
1 + 1 = 2
I figured I would have to load the same way you showed me above. But how would I recall the records to display on the same line?
If you can help with that too. That would be fantastic.
Leo
-
Tuesday, March 27, 2012 2:00 AMThe XmlNodeList is the result of a SelectNodes call. So you could loop over all the data elements like the following:dim doc as new XmlDocument()doc.Load("SomeFile.xml")for each node as XmlNode in doc.SelectNodes("//DataNodes/Data")' build the stringnextNow the step building the string is done by using the values for each node below the current data node. That can be done by concatenating strings like: node ["numOne"].InnerText & node ["StrOperator"].InnerText, etc... for each sub element under the Data element.Hopefully that helps.
--
Mike -
Tuesday, March 27, 2012 2:35 AM
Hi Mike.
I was not able to concatenating the strings at all. Not sure exactly why. Try to look on msdn but did not find anything there as well.
Not sure if the syntax is quite right as well. Will there be any other way We can do it? I would think that this would be the best way if the syntax is correct.
Appreciate...
Leo
-
Tuesday, March 27, 2012 8:51 PM
-
Friday, March 30, 2012 2:04 PM
Hi Mike,
I think I figured this one out. Sorry for the delay in responding, I had a couple things going at the same time. Here's what I tried and got it going:
'loads records into listbox' saveXML = CType(saveXML.Load(filename, saveXML.GetType()), SaveXML) For Each node As SaveXML.Data In saveXML.DataNodes lstDisplay.Items.Add(String.Format("{0} {1} {2} {3} {4}",
node.numOne, node.StrOperator, node.numTwo, node.StrEqual, node.result)) Next
Hope this helps somebody else that runs into the same problems that I did.
Thanks for your help. It definetely stirred me on the right direction.
Leo

