Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XML Search

XML Search

Scheduled Pinned Locked Moved C#
xmlhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DJ245
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • D DJ245

      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

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • P Pete OHanlon

        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

        D Offline
        D Offline
        DJ245
        wrote on last edited by
        #3

        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?

        P P 2 Replies Last reply
        0
        • D DJ245

          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?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • D DJ245

            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?

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups