tags:

views:

35

answers:

1

hi I am searching the xml files from the file system using the dollowing query. Everything works fine except the following situation. If i have 2 xml files and one dosen have a specific element and other one has.

My condition in the query is if element is empty or null load the xml file[Is empty==true] case "Is Empty":

 if(xvalue == "True")
                            {
                                _xmlFilter = _xmlFilter = _xmlFilter.And(x => x.Element(xname) == null || x.Element(xname).IsEmpty);
                            }
                            else
                            {
                                _xmlFilter = _xmlFilter.And(x => x.Element(xname) != null && (!string.IsNullOrEmpty(x.Element(xname).Value)));
                            }     

If "Is Empty" == false it works fine.

But the first condition returns both the files. Any help? thanks in advance

A: 

Expression> _xmlFilter = x => true; if(Filtersource.Rows.Count > 0) { if(this.Filtersource.Rows[0]["Property"].ToString().Trim() != "" && this.Filtersource.Rows[0]["Operator"].ToString().Trim() != "") { foreach(DataRow row in Filtersource.Rows) { string xname = null; string xvalue = null; //DataRow locaRow = row; xname = row["Property"].ToString(); xvalue = GetEncodedString(row["Value"].ToString());
switch(row["Operator"].ToString()) { case "Contains": _xmlFilter = _xmlFilter.And(x => x.Element(xname) != null && x.Element(xname).Value.ToLower().Contains(xvalue.ToLower())); break; case "Not Contains": _xmlFilter = _xmlFilter.And(x => x.Element(xname) != null && x.Element(xname).Value.IndexOf(GetEncodedString(xvalue), StringComparison.InvariantCultureIgnoreCase) == -1); break; case "Equals": _xmlFilter = _xmlFilter.And(x => x.Element(xname) != null && x.Element(xname).Value.Equals(GetEncodedString(xvalue), StringComparison.InvariantCultureIgnoreCase)); break; case "Not Equals": _xmlFilter = _xmlFilter.And(x => x.Element(xname) != null && string.Compare(x.Element(xname).Value, GetEncodedString(xvalue), true) != 0); break; case "Is Empty": //_xmlFilter = _xmlFilter.And(x => (x.Element(row["Property"].ToString()).Value.Trim().Length==0)); //_xmlFilter = _xmlFilter.And(x => (x.Element(row["Property"].ToString())==null) || string.IsNullOrEmpty(x.Element(row["Property"].ToString()).Value)); //_xmlFilter = _xmlFilter.And(x => (x.Element(xname) == null)); //_xmlFilter = _xmlFilter.And(x => x.Element(xname) != null && string.IsNullOrEmpty(x.Element(xname).Value).ToString() == GetEncodedString(xvalue)); if(xvalue == "True") { _xmlFilter = _xmlFilter.And(x => x.Element(xname).IsEmpty || x.Element(xname) == null); } else { _xmlFilter = _xmlFilter.And(x => x.Element(xname) != null && (!string.IsNullOrEmpty(x.Element(xname).Value))); }
break; } }

Ravi