Marc Davis
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</LastNa me>
<SSN>123456789</SSN>
<State>OH</State>
<LandLinePhone>1112223456< /LandLineP hone>
<VOIPPhone>9876543211</VOI PPhone>
</Test>
But I have another XML (Test2.xml) like:
<Test>
<FirstName>Testa</FirstNam e>
<LastName>LastTest2</LastN ame>
<SSN>192837465</SSN>
<State>CO</State>
<LandLinePhone>1112223457< /LandLineP hone>
</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
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</LastNa
<SSN>123456789</SSN>
<State>OH</State>
<LandLinePhone>1112223456<
<VOIPPhone>9876543211</VOI
</Test>
But I have another XML (Test2.xml) like:
<Test>
<FirstName>Testa</FirstNam
<LastName>LastTest2</LastN
<SSN>192837465</SSN>
<State>CO</State>
<LandLinePhone>1112223457<
</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
ASKER
I thought I did that. What the heck did I do? Let me try that again.
ASKER
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().Cont ains("Phon e") && 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?
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().Cont
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)22345 7</LandLin ePhone>
Or do you want a bool return if any phone number matches the above line?
Fernando
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)22345
Or do you want a bool return if any phone number matches the above line?
Fernando
ASKER
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?
Is there/would there be a way to do the Console.WriteLine for the parent node?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
This code snippet will do what you need.
Open in new window
Fernando