Link to home
Start Free TrialLog in
Avatar of Marc Davis
Marc DavisFlag for United States of America

asked on

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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
Avatar of Marc Davis

ASKER

I thought I did that. What the heck did I do? Let me try that again.
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?                                
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
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?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
Not a problem, glad to help.