AlexPonnath
asked on
VB.NET and XML parsing
Ok below is a rough layout of my file i am trying to parse. I was wondering how i can loop thru all Call elements /node in the CDR's
to extract its values. Also how can i check if the calls node has subnotes since that is not always the case. to make it more clear i attached the complete sample data
<File>
<FileHeader></FileHeader>
<CDRs>
<Call><RoutingInfo></Routi ngInfo> </Call>
<Call> </Call>
</CDRs>
<FileFooter></FileFooter>
</File>
to extract its values. Also how can i check if the calls node has subnotes since that is not always the case. to make it more clear i attached the complete sample data
<File>
<FileHeader></FileHeader>
<CDRs>
<Call><RoutingInfo></Routi
<Call> </Call>
</CDRs>
<FileFooter></FileFooter>
</File>
ASKER
I am a bit confused here, for one or the other reason neither my or your code works against my file..
sample2.xml
sample2.xml
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks, that somewhat proofs why my code didn't work. But i don't want to use Linq or datasets since i have existing logic which handles my parsing. The only problem is it doesn't work with namespace.
So what do i have to do to be able to pretty much read my XML file, and get all Call nodes so i can process them and find subnodes etc.
So what do i have to do to be able to pretty much read my XML file, and get all Call nodes so i can process them and find subnodes etc.
Please see your other post.
Hi Alex;
To your statement, " But i don't want to use Linq or datasets since i have existing logic which handles my parsing", this was not stated in this question and therefore you got a generalized code that would do the job. Seeming you now know why my code snippet gave you issues was with the Xml Namespace that was not stated in the original question but once you posted the actual schema of the XML the updated code snippet solved the issue.
Please close this question with the solution that worked and if you still have issues with the more specific code please post in the other question you have on this subject.
Thank you and have a great day.
To your statement, " But i don't want to use Linq or datasets since i have existing logic which handles my parsing", this was not stated in this question and therefore you got a generalized code that would do the job. Seeming you now know why my code snippet gave you issues was with the Xml Namespace that was not stated in the original question but once you posted the actual schema of the XML the updated code snippet solved the issue.
Please close this question with the solution that worked and if you still have issues with the more specific code please post in the other question you have on this subject.
Thank you and have a great day.
From your XML file shown here re-formatted.
Open in new window
Using Linq to XML you can get a list of values from your document so that you do not have to loop through the nodes. The question is what values do you want to have returned to you, for example will they have Attributes or just inner text and which ones you want back? For the Call nodes it would be desirable to have attributes and not inner text because if you return the inner text of those that have child elements the text from the child elements will also be in the Call node value. This would not be the case for the RoutingInfo nodes because they do not have children.To your question, "how can i check if the calls node has subnotes since that is not always the case", Linq to XML has a method HasElements which returns a Boolean if it has True and False for no. The following code snippet show one way to do what you need.
Open in new window
Using the Following XML document.Open in new window
From Visual Studio the results look like the following.