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. Searching a particular node in XML

Searching a particular node in XML

Scheduled Pinned Locked Moved C#
algorithmsxmlquestion
9 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.
  • S Offline
    S Offline
    Shankar Balaji
    wrote on last edited by
    #1

    ... .. .. If , we have to get the node corresponding to the Answers node with id = 1 , how can we do that? Shankar C

    P M 2 Replies Last reply
    0
    • S Shankar Balaji

      ... .. .. If , we have to get the node corresponding to the Answers node with id = 1 , how can we do that? Shankar C

      P Offline
      P Offline
      Polis Pilavas
      wrote on last edited by
      #2

      A good idea would be to load the contents of your XML file into a dataSet and do your searching on the dataSet instead. This gives you the advantage of handling XML data as if it was an ordinary Database. Regards, Polis Can you practice what you teach?

      S 1 Reply Last reply
      0
      • P Polis Pilavas

        A good idea would be to load the contents of your XML file into a dataSet and do your searching on the dataSet instead. This gives you the advantage of handling XML data as if it was an ordinary Database. Regards, Polis Can you practice what you teach?

        S Offline
        S Offline
        Shankar Balaji
        wrote on last edited by
        #3

        private void ImportXML(string path) { XmlTextReader reader = null; try { reader = new XmlTextReader(Server.MapPath("../CheckList/"+strFileName)); } catch { Response.Write("File Does'nt exists"); } XmlDocument doc = new XmlDocument(); doc.Load(reader); reader.Close(); XmlDocument docNew = new XmlDocument(); docNew.Load(path); XmlNode oldCd = null; bool Append = false; XmlElement root = doc.DocumentElement; oldCd = root.SelectSingleNode("//answers[@objectid=" + intValue + "]"); if(oldCd == null) Append = true; XmlNode CdNew; XmlElement rootNew = docNew.DocumentElement; CdNew = rootNew.SelectSingleNode("/answers[@objectid=" + intValue + "]"); XmlNode newBook = doc.ImportNode(CdNew, true); XmlNode newNode = doc.DocumentElement.ChildNodes[1]; if(Append) newNode.AppendChild(newBook); else newNode.ReplaceChild(newBook,oldCd); try { doc.Save(Server.MapPath("../CheckList/"+strFileName)); } catch { string strPath = Server.MapPath("../CheckList/") + "TempFile.xml"; ImportXML(strPath); } } can u help me in another way to do this?

        P 1 Reply Last reply
        0
        • S Shankar Balaji

          private void ImportXML(string path) { XmlTextReader reader = null; try { reader = new XmlTextReader(Server.MapPath("../CheckList/"+strFileName)); } catch { Response.Write("File Does'nt exists"); } XmlDocument doc = new XmlDocument(); doc.Load(reader); reader.Close(); XmlDocument docNew = new XmlDocument(); docNew.Load(path); XmlNode oldCd = null; bool Append = false; XmlElement root = doc.DocumentElement; oldCd = root.SelectSingleNode("//answers[@objectid=" + intValue + "]"); if(oldCd == null) Append = true; XmlNode CdNew; XmlElement rootNew = docNew.DocumentElement; CdNew = rootNew.SelectSingleNode("/answers[@objectid=" + intValue + "]"); XmlNode newBook = doc.ImportNode(CdNew, true); XmlNode newNode = doc.DocumentElement.ChildNodes[1]; if(Append) newNode.AppendChild(newBook); else newNode.ReplaceChild(newBook,oldCd); try { doc.Save(Server.MapPath("../CheckList/"+strFileName)); } catch { string strPath = Server.MapPath("../CheckList/") + "TempFile.xml"; ImportXML(strPath); } } can u help me in another way to do this?

          P Offline
          P Offline
          Polis Pilavas
          wrote on last edited by
          #4

          Load data from the XML document into a dataSet:

          ds
          

          Create a new row based on the table of the dataSet

          DataRow drs= ds.Tables["Table1"].NewRow();
          //Construct field values for the new row
          drs["Message"] = "Hello World!"
          //Add the new row into the dataTable
          ds.Tables[table].Rows.Add(drs);

          Delete an existing row from the table

          if (ds.Tables["Table1"].Rows.Count > 0)
          {
          //Remove the selected row from the dataSet (assuming we use a dataGrid)
          ds.Tables[table].Rows.RemoveAt(this.dataGrid1.CurrentCell.RowNumber);
          }

          Search for a value

          if (ds.Tables["Table1"].Rows.Count == 1)
          if (ds.Tables["Table1"].Rows[0][ds.Tables["Table1"].Columns[0]].ToString() == "Hello World!")
          MessageBox.Show("Found");

          Save the changes to the XML file

          ds.WriteXml(fileName);

          Hope this will help you get the picture. Regards, Polis Can you practice what you teach?

          S 1 Reply Last reply
          0
          • P Polis Pilavas

            Load data from the XML document into a dataSet:

            ds
            

            Create a new row based on the table of the dataSet

            DataRow drs= ds.Tables["Table1"].NewRow();
            //Construct field values for the new row
            drs["Message"] = "Hello World!"
            //Add the new row into the dataTable
            ds.Tables[table].Rows.Add(drs);

            Delete an existing row from the table

            if (ds.Tables["Table1"].Rows.Count > 0)
            {
            //Remove the selected row from the dataSet (assuming we use a dataGrid)
            ds.Tables[table].Rows.RemoveAt(this.dataGrid1.CurrentCell.RowNumber);
            }

            Search for a value

            if (ds.Tables["Table1"].Rows.Count == 1)
            if (ds.Tables["Table1"].Rows[0][ds.Tables["Table1"].Columns[0]].ToString() == "Hello World!")
            MessageBox.Show("Found");

            Save the changes to the XML file

            ds.WriteXml(fileName);

            Hope this will help you get the picture. Regards, Polis Can you practice what you teach?

            S Offline
            S Offline
            Shankar Balaji
            wrote on last edited by
            #5

            Thanx polis, but can u tell about the ImportNodes method in the XML api?

            P 1 Reply Last reply
            0
            • S Shankar Balaji

              Thanx polis, but can u tell about the ImportNodes method in the XML api?

              P Offline
              P Offline
              Polis Pilavas
              wrote on last edited by
              #6

              ImportNodes method? Hmm... I am not aware of this one Shankar, sorry. XML API? What exactly do you mean by this? Regards, Polis Can you practice what you teach?

              S 1 Reply Last reply
              0
              • P Polis Pilavas

                ImportNodes method? Hmm... I am not aware of this one Shankar, sorry. XML API? What exactly do you mean by this? Regards, Polis Can you practice what you teach?

                S Offline
                S Offline
                Shankar Balaji
                wrote on last edited by
                #7

                In the System.XML namespace , we have a class called XMLDocument. If we create an object for the above said class, we can call the ImportNodes method. It's working correctly. but i need to know the correct usage for importnodes method.

                P 1 Reply Last reply
                0
                • S Shankar Balaji

                  In the System.XML namespace , we have a class called XMLDocument. If we create an object for the above said class, we can call the ImportNodes method. It's working correctly. but i need to know the correct usage for importnodes method.

                  P Offline
                  P Offline
                  Polis Pilavas
                  wrote on last edited by
                  #8

                  Oh right. I just had a quick read on the MSDN[^]about the ImportNode() method. Well, it seems that all it does is to import specified XML nodes from another XML file into the one you are working with. E.g. XML File #1 (Current XML file you work with) Nodes: FirstName, LastName, Age XML File #2 Nodes: FirstName, LastName, Age, Address By saying something like "....ImportNode(doc2.DocumentElement.LastChild, true);", it will append the Address node into your XML file #1. Regards, Polis Can you practice what you teach?

                  1 Reply Last reply
                  0
                  • S Shankar Balaji

                    ... .. .. If , we have to get the node corresponding to the Answers node with id = 1 , how can we do that? Shankar C

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #9

                    My article Accessing XML using insert/update/delete/query statements[^] might be able to help--it should be really easy! Marc MyXaml Advanced Unit Testing YAPO

                    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