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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.