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. XML / XSL
  4. Remove a XMLnode - Pls help

Remove a XMLnode - Pls help

Scheduled Pinned Locked Moved XML / XSL
xmlhelp
4 Posts 2 Posters 4 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.
  • H Offline
    H Offline
    harvid
    wrote on last edited by
    #1

    Hello, Below is my XML document. FP108MR 0 Z9999 I need to remove only node so that my xml document remains as below. FP108MR 0 Z9999 I used the following code to convert Dataset to a XMLdocument. Dim dtCounter As New DataSet("GetCountsResult") oDatabase.Connect() oDatabase.PopulateDataSet("Count", "Get_Current_Counter", True, dtCounter) Dim sw As New StringWriter dtCounter.WriteXml(sw, XmlWriteMode.IgnoreSchema) Dim xmldoc As New XmlDocument xmldoc.LoadXml(sw.ToString()) Please help me in getting this. Thanks.

    V 1 Reply Last reply
    0
    • H harvid

      Hello, Below is my XML document. FP108MR 0 Z9999 I need to remove only node so that my xml document remains as below. FP108MR 0 Z9999 I used the following code to convert Dataset to a XMLdocument. Dim dtCounter As New DataSet("GetCountsResult") oDatabase.Connect() oDatabase.PopulateDataSet("Count", "Get_Current_Counter", True, dtCounter) Dim sw As New StringWriter dtCounter.WriteXml(sw, XmlWriteMode.IgnoreSchema) Dim xmldoc As New XmlDocument xmldoc.LoadXml(sw.ToString()) Please help me in getting this. Thanks.

      V Offline
      V Offline
      VJ Reddy
      wrote on last edited by
      #2

      The ReplaceChild method of XmlNodeclass explained here http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.replacechild.aspx[^] can be used for replacing the desired nodes as shown below:

      void Main()
      {
      string xmlCounters =
      @"<GetCountersResult>
      <GetCountsResult xmlns="""">
      <Count>
      <ID>FP108MR</ID>
      <Value>0</Value>
      <Part>Z9999</Part>
      </Count>
      </GetCountsResult>
      <GetCountsResult xmlns="""">
      <Count>
      <ID>FP108MR No2</ID>
      <Value>0 No2</Value>
      <Part>Z9999 No2</Part>
      </Count>
      </GetCountsResult>
      </GetCountersResult>";

      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.LoadXml(xmlCounters);
      var countNodes = xmlDoc.GetElementsByTagName("Count");
      //foreach throws, the element list has changed, error
      for (int i=0; i < countNodes.Count; i++)
      {
          xmlDoc.DocumentElement.ReplaceChild(countNodes\[i\], countNodes\[i\].ParentNode);
      }
      string fileName = @"F:\\ModifiedXml.xml";
      xmlDoc.Save(fileName);
      

      }

      //The contents of file

      // <GetCountersResult>
      // <Count>
      // <ID>FP108MR</ID>
      // <Value>0</Value>
      // <Part>Z9999</Part>
      // </Count>
      // <Count>
      // <ID>FP108MR No2</ID>
      // <Value>0 No2</Value>
      // <Part>Z9999 No2</Part>
      // </Count>
      // </GetCountersResult>

      H 1 Reply Last reply
      0
      • V VJ Reddy

        The ReplaceChild method of XmlNodeclass explained here http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.replacechild.aspx[^] can be used for replacing the desired nodes as shown below:

        void Main()
        {
        string xmlCounters =
        @"<GetCountersResult>
        <GetCountsResult xmlns="""">
        <Count>
        <ID>FP108MR</ID>
        <Value>0</Value>
        <Part>Z9999</Part>
        </Count>
        </GetCountsResult>
        <GetCountsResult xmlns="""">
        <Count>
        <ID>FP108MR No2</ID>
        <Value>0 No2</Value>
        <Part>Z9999 No2</Part>
        </Count>
        </GetCountsResult>
        </GetCountersResult>";

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlCounters);
        var countNodes = xmlDoc.GetElementsByTagName("Count");
        //foreach throws, the element list has changed, error
        for (int i=0; i < countNodes.Count; i++)
        {
            xmlDoc.DocumentElement.ReplaceChild(countNodes\[i\], countNodes\[i\].ParentNode);
        }
        string fileName = @"F:\\ModifiedXml.xml";
        xmlDoc.Save(fileName);
        

        }

        //The contents of file

        // <GetCountersResult>
        // <Count>
        // <ID>FP108MR</ID>
        // <Value>0</Value>
        // <Part>Z9999</Part>
        // </Count>
        // <Count>
        // <ID>FP108MR No2</ID>
        // <Value>0 No2</Value>
        // <Part>Z9999 No2</Part>
        // </Count>
        // </GetCountersResult>

        H Offline
        H Offline
        harvid
        wrote on last edited by
        #3

        Thank you so much. I will try this.

        V 1 Reply Last reply
        0
        • H harvid

          Thank you so much. I will try this.

          V Offline
          V Offline
          VJ Reddy
          wrote on last edited by
          #4

          You're welcome. After trying please give your feedback and if it is helpful you may consider to vote. Thank you for the response.

          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