XML Search
-
Dear all, In my application i have an XML like this.. I want to perform a search in this XML Based on SubID, ModuleId, TaskName, between start and end dates. Please help me. Also i need all the parents of the searched nodes without siblings.
Please help Regards, DJ
-
Dear all, In my application i have an XML like this.. I want to perform a search in this XML Based on SubID, ModuleId, TaskName, between start and end dates. Please help me. Also i need all the parents of the searched nodes without siblings.
Please help Regards, DJ
As they are attributes, you need to use attribute based searches in your XPath. The type of syntax you are looking to use is
//[@...=value]
. You are going to have a problem with the date part as this stands because your data is all text as far as the parser is concerned. In order to work with date information, you are going to have to provide a schema and set the data types as appropriate. BTW, your XML is malformed - you must enclose your values in quotes, so having TaskId=9 is incorrect and it should actually be TaskId="9"Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
As they are attributes, you need to use attribute based searches in your XPath. The type of syntax you are looking to use is
//[@...=value]
. You are going to have a problem with the date part as this stands because your data is all text as far as the parser is concerned. In order to work with date information, you are going to have to provide a schema and set the data types as appropriate. BTW, your XML is malformed - you must enclose your values in quotes, so having TaskId=9 is incorrect and it should actually be TaskId="9"Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Re: XML Search Ok.. fine.. Updated the XML taskXML =
And perform the search criteria as follows (My code)
public XMLNode searchXML()
{
if ((null != taskXML) && 0 < subProjectId)
{
taskXML = taskXML.SelectSingleNode("//Task[@SubId ='" + subProjectId.ToString() + "']");
}
if ((null != taskXML) && 0 < moduleId)
{
taskXML = taskXML .SelectSingleNode("//Task[@TaskId ='" + moduleId.ToString() + "']");
}//Problem starts
if ((null != taskXML) && (null != startDate && null != endDate))
{
taskXML= taskXML.SelectSingleNode("//Task[@StartDate >'" + startDate.ToString() + "']");
if (null != taskXML)
taskXML= taskXML.SelectSingleNode("//Task[@StartDate < '" + endDate.ToString() + "']");
}
//Problem end
if ((null != taskXML) && 0 < statusId)
{
taskXML = taskXML .SelectSingleNode("//Task[@StatusId='" + statusId + "']");
}//Problem starts
if ((null != projectTaskXML) && string.Empty != taskName)
{
taskXML = taskXML .SelectSingleNode("//Task[@TaskName='" + taskName + "']");
}
//Problem end
return taskXML;
}But the date search and name search not working. Also i need to get the parents of searched tasks Could u please help me?
-
Re: XML Search Ok.. fine.. Updated the XML taskXML =
And perform the search criteria as follows (My code)
public XMLNode searchXML()
{
if ((null != taskXML) && 0 < subProjectId)
{
taskXML = taskXML.SelectSingleNode("//Task[@SubId ='" + subProjectId.ToString() + "']");
}
if ((null != taskXML) && 0 < moduleId)
{
taskXML = taskXML .SelectSingleNode("//Task[@TaskId ='" + moduleId.ToString() + "']");
}//Problem starts
if ((null != taskXML) && (null != startDate && null != endDate))
{
taskXML= taskXML.SelectSingleNode("//Task[@StartDate >'" + startDate.ToString() + "']");
if (null != taskXML)
taskXML= taskXML.SelectSingleNode("//Task[@StartDate < '" + endDate.ToString() + "']");
}
//Problem end
if ((null != taskXML) && 0 < statusId)
{
taskXML = taskXML .SelectSingleNode("//Task[@StatusId='" + statusId + "']");
}//Problem starts
if ((null != projectTaskXML) && string.Empty != taskName)
{
taskXML = taskXML .SelectSingleNode("//Task[@TaskName='" + taskName + "']");
}
//Problem end
return taskXML;
}But the date search and name search not working. Also i need to get the parents of searched tasks Could u please help me?
I already told you that your date search wouldn't work because the data type is a string, and you need to create a schema for this. If you had read my answer fully, you'd have seen that vital piece of information.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Re: XML Search Ok.. fine.. Updated the XML taskXML =
And perform the search criteria as follows (My code)
public XMLNode searchXML()
{
if ((null != taskXML) && 0 < subProjectId)
{
taskXML = taskXML.SelectSingleNode("//Task[@SubId ='" + subProjectId.ToString() + "']");
}
if ((null != taskXML) && 0 < moduleId)
{
taskXML = taskXML .SelectSingleNode("//Task[@TaskId ='" + moduleId.ToString() + "']");
}//Problem starts
if ((null != taskXML) && (null != startDate && null != endDate))
{
taskXML= taskXML.SelectSingleNode("//Task[@StartDate >'" + startDate.ToString() + "']");
if (null != taskXML)
taskXML= taskXML.SelectSingleNode("//Task[@StartDate < '" + endDate.ToString() + "']");
}
//Problem end
if ((null != taskXML) && 0 < statusId)
{
taskXML = taskXML .SelectSingleNode("//Task[@StatusId='" + statusId + "']");
}//Problem starts
if ((null != projectTaskXML) && string.Empty != taskName)
{
taskXML = taskXML .SelectSingleNode("//Task[@TaskName='" + taskName + "']");
}
//Problem end
return taskXML;
}But the date search and name search not working. Also i need to get the parents of searched tasks Could u please help me?
The date search might work if you use the ISO 8601-compliant format
YYYY-DD-MM
-- which is what the W3C recommends for XML anyway.