Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,

I need to validate an output XML. Output XMl generated based on a search value.

how the code works

1) if the user types a name in the textbox and invoke the service it will generate an output Xml.
2) Search behaves likes a "Start with manner"
3) It should search 3 columns lastname,sortname,corpname. If any of the columns contains the value it should return.

My requirement is to validate the output xml
1) check 3 child elements (lastname,sortname,corpname) and verify in at least on of the element contain the search value. if not its a fail case

here is my XML

XML
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <info>
    <lastname>jinesh</lastname>
    <sortname>sam</sortname>
    <corpname>kp</corpname>
    <company>asdf</company>
    <address>qwert</address>
    <pincode>786678</pincode>
  </info>
  <info>
    <lastname>manoj</lastname>
    <sortname>jinu</sortname>
    <corpname>kalpana</corpname>
    <company>zxcv</company>
    <address>bnml</address>
    <pincode>12345</pincode>
  </info>
  <info>
    <lastname>karthik</lastname>
    <sortname>rahul</sortname>
    <corpname>jimmy</corpname>
    <company>lkjh</company>
    <address>poiu</address>
    <pincode>98765</pincode>
  </info>
  <info>
    
  </info>
  
</Root>


Here this xml is valid for three inputs 'ji','j','k'

Here is my code
C#
public static bool check(string namecheck)
        {
        XDocument xdoc = XDocument.Load("Samplexml.xml");
        return xdoc.Descendants("info")
                .Where(item =>
                    {
                        string fname = (string)item.Element("lastname") ?? "";
                        string sname = (string)item.Element("sortname") ?? "";
                        string cname = (string)item.Element("corpname") ?? "";
                        return fname.StartsWith(namecheck) && sname.StartsWith(namecheck) && cname.StartsWith(namecheck);
                    })
                    .ToList().Any();        
        }


C#
bool tf=check("ji");
Posted
Comments
Abhipal Singh 26-Jul-15 14:16pm    
And what is the issue you are facing with this code?

I think && should be replaced with || in the return statement below:
return fname.StartsWith(namecheck) &&|| sname.StartsWith(namecheck) &&|| cname.StartsWith(namecheck);
jinesh sam 26-Jul-15 14:21pm    
return value received is false. if i pass "ji" it should be pass
let me try with ur suggestion
jinesh sam 26-Jul-15 14:27pm    
now i am getting return value as true. but the problem is when i pass "jin" it should return false because 3rd info node does not contain name starting with "jin"

1 solution

As Abhipal Singh[^] suggested, you have to replace && (boolean AND) with || (boolean OR) operant. Why? Test it to find out:
C#
var res1 = true && true && true; //returns true
var res2 = true && true && false; //returns false
var res3 = true && false && false; //returns false
var res4 = false && false && false; //returns false
var res5 = true || true || true; //returns true
var res6 = true || true || false; //returns true
var res7 = true || false || false; //returns true
var res8 = false || false || false; //returns false


Below query works as well:
C#
string sSearchedValue = @"jin";
var result = xdoc.Root.Descendants("info")
    .Where(x=>x.HasElements)
    .Select(x=>x.Element("lastname").Value.StartsWith(sSearchedValue) ||
        x.Element("sortname").Value.StartsWith(sSearchedValue) ||
        x.Element("corpname").Value.StartsWith(sSearchedValue));

Returns:
True
True
False


If you would like to get corresponding nodes, replace Select with Where statement.
Returned values:
XML
<info>
  <lastname>jinesh</lastname> <!-- matching value -->
  <sortname>sam</sortname>
  <corpname>kp</corpname>
  <company>asdf</company>
  <address>qwert</address>
  <pincode>786678</pincode>
</info>
<info>
  <lastname>manoj</lastname>
  <sortname>jinu</sortname> <!-- matching value -->
  <corpname>kalpana</corpname>
  <company>zxcv</company>
  <address>bnml</address>
  <pincode>12345</pincode>
</info>
 
Share this answer
 
Comments
jinesh sam 27-Jul-15 14:49pm    
@Maciej Thanks:) Actually i don't need to get the output as true,true,false.
if every thing is true. Its need to return single true or any false it should return false.How can i modify further
jinesh sam 27-Jul-15 15:21pm    
.ToList().Any(i => !i);
This helps for me
again thanks for helping
Maciej Los 27-Jul-15 15:36pm    
You're very welcome. Please, accept my answer if it was helpful...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900