Solved

VB.NET and XML parsing

Posted on 2016-08-14
6
113 Views
Last Modified: 2016-08-16
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></RoutingInfo> </Call>
          <Call> </Call>
       </CDRs>
      <FileFooter></FileFooter>
</File>
0
Comment
Question by:AlexPonnath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41755880
Hi

From your XML file shown here re-formatted.
<File>
  <FileHeader></FileHeader>
    <CDRs>
      <Call>
        <RoutingInfo>
        </RoutingInfo>
      </Call>
      <Call> </Call>
    </CDRs>
  <FileFooter></FileFooter>
</File>

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.
'' Load the XML into memory from the file system
Dim xdoc = XDocument.Load("Data.xml")
'' Get all the Call nodes with children if they have any
Dim results = (From n In xdoc.Root.Descendants("Call")
               Select New With {
                  .CallVal = n.Attribute("value").Value,
                  .HasChildren = n.HasElements
               }).ToList

Open in new window

Using the Following XML document.
<File>
  <FileHeader></FileHeader>
    <CDRs>
      <Call value="1">
        <RoutingInfo>
        	Routing Value
        </RoutingInfo>
      </Call>
      <Call value="2"></Call>
    </CDRs>
  <FileFooter></FileFooter>
</File>

Open in new window

From Visual Studio the results look like the following.How the results look like
0
 

Author Comment

by:AlexPonnath
ID: 41756739
I am a bit confused here, for one or the other reason neither my or your code works against my file..
sample2.xml
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41756812
Hi Alex;

The reason that neither your code or my code works with the XML that you are using is that the XML you are using contains a name space attribute on the document. In order to get the results you need to reference that name space. Here is my code modified to work with the name space that is defined in the Root element.
'' Load the XML into memory from the file system
Dim xdoc = XDocument.Load("CallData.xml")
Dim ns = xdoc.Root.Name.Namespace

'' Get all the Call nodes with children if they have any
Dim results = (From n In xdoc.Root.Descendants(ns + "Call")
               Select New With {
                  .CallVal = n.Attribute("seqnum").Value,
                  .HasChildren = n.HasElements
               }).ToList

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:AlexPonnath
ID: 41757263
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.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41757266
Please see your other post.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41757834
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question