LINQ and C# and wildcard for existing XML node...

Hi,

I'm not very familiar with LINQ but this might be relatively simple.

I have an XML file (Test1.xml) like

<Test>
   <FirstName>Test</FirstName>
   <LastName>LastTest</LastName>
   <SSN>123456789</SSN>
   <State>OH</State>
   <LandLinePhone>1112223456</LandLinePhone>
   <VOIPPhone>9876543211</VOIPPhone>
</Test>
 
But I have another XML (Test2.xml) like:

<Test>
   <FirstName>Testa</FirstName>
   <LastName>LastTest2</LastName>
   <SSN>192837465</SSN>
   <State>CO</State>
   <LandLinePhone>1112223457</LandLinePhone>
</Test>

Notice the VOIP phone does not exist in the second. However, what I need it to do is search the *nodes* that have the term "Phone" in it.

I can go through the directory and get each of the files.

But I'm not too sure how to use LINQ to check for the existence of the node with the wild card.

How can that be done? I'm using C#.

Any information would be greatly appreciated.

Thanks
LVL 1
davismAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi davism;

Using the Parent method on a node will give you access to the nodes parent itself. See code snippet below using a modified code you posted.

var nodesFound = (from Phone in root.Descendants()
                  where Phone.Name.ToString().Contains("Phone") && Phone.Value.Contains("()")
                  select Phone); 
                   
foreach( var node in nodesFound )
{
    Console.WriteLine( node.Parent(....);
}

Open in new window


Fernando
0
 
Fernando SotoRetiredCommented:
Hi davism;

This code snippet will do what you need.

using System.Xml.Linq;

// Directory to search
DirectoryInfo dInfo = new DirectoryInfo(@"C:\Temp");
// XML files to be processed
FileInfo[] fInfo = dInfo.GetFiles("*.xml");
             
foreach (var file in fInfo)
{
    // Get XML Documents one by one
    XDocument xdoc = XDocument.Load(file.FullName);
    var phones = from xml in xdoc.Descendants()
                 where xml.Name.ToString().Contains("Phone")
                 select xml;
    // Display all node that have the word Phone in its tag name                 
    foreach (var node in phones)
    {
        Console.WriteLine(node.Value);
    }
}

Open in new window


Fernando
0
 
davismAuthor Commented:
I thought I did that. What the heck did I do? Let me try that again.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
davismAuthor Commented:
Yep...I did try something like that and didn't work being the Select Phone; last part didn't  convert to bool.

What I have is this:

//Implement the namespace as defined in the root node.
                        XNamespace ns = root.GetDefaultNamespace();

                        //Implement the namespace as defined in the root node.
                        XNamespace ns = root.GetDefaultNamespace();

                        bool bolFound = (from Phone in root.Descendants()
                                         where Phone.Name.ToString().Contains("Phone") && Phone.Value.Contains("()")
                                            select Phone);  

The select Phone is going to return back not a bool which is causing a problem I would imagine.

I'm trying to find out if the situation exists in any node that has the "Phone" in the name and a "(".      

How can I get that?                                
0
 
Fernando SotoRetiredCommented:
Hi davism;

In the original question there is no mention of a namespace? So is there a namespace to consider?

In your query you are returning and collection of XML node and will fail because you are attempting to place it in a variable of type bool.

So the question now is that you want returned only the phone numbers that are formatted like in the following line?

<LandLinePhone>1(112)223457</LandLinePhone>

Or do you want a bool return if any phone number matches the above line?

Fernando
0
 
davismAuthor Commented:
I actually got it working a little more without the boolean type because I realized there was value in specifying the node as well.

Is there/would there be a way to do the Console.WriteLine for the parent node?
0
 
davismAuthor Commented:
I worked a few things in addition to what you provided and got it working but great information! Thank you very much and much appreciated!
0
 
Fernando SotoRetiredCommented:
Not a problem, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.