none
Find replace in XML Document

    General discussion

  • Hi all,

    Following is my XML

    < Import>
       <File FileValue="00000A96.dat" ></File> 
       <File FileValue="00000A98.dat" ></File>
       <File FileValue="00000AA4.dat" ></File>
       <File FileValue="00000AA5.dat" ></File>
       <File FileValue="00000AAA.dat" ></File>
     </Import>

    When I searched multiple text like "00000A96.dat" and "00000AAA.dat" and by the result both Element should me removed.

    And note: my XML file is around 200+MB in size how to process this?


    Thanks Vivek SharePoint Foundation 2010 Book
    http://www.redpipit.com

    Sunday, August 18, 2013 6:20 PM

All replies

  • Hello,

    Welcome toMSDN Support Forum.

    From your description, I notice the issue you are experiencing is how to filter a XML file.

    If I have any misunderstood, please let me know.

    As far as I know that we can use Linq-To-Xml to operate the xml file.

    With your Xml file I made a sample and please see the sample code below:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Xml.Linq;
    namespace LinqToXml
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                string[] nodeRemove = new string[2] { "00000A96.dat", "00000AAA.dat" };
                string path = "E:\\BMX\\Lab\\SmapleFile\\XMLFileTest.xml";
                XElement root = XElement.Load(path);
                for (int i = 0; i < nodeRemove.Length; i++)
                {
                    root.Elements("File").Where((f => f.Attribute("FileValue").Value == nodeRemove[i])).Remove();
                }
                root.Save(path);
            }
        }
    }

    Before run the program, the Xml file is like:

    <Import>
      <File FileValue="00000A96.dat" ></File>
      <File FileValue="00000A98.dat" ></File>
      <File FileValue="00000AA4.dat" ></File>
      <File FileValue="00000AA5.dat" ></File>
      <File FileValue="00000AAA.dat" ></File>
    </Import>

    And after run the program, the result:

    <?xml version="1.0" encoding="utf-8"?>
    <Import>
      <File FileValue="00000A98.dat"></File>
      <File FileValue="00000AA4.dat"></File>
      <File FileValue="00000AA5.dat"></File>
    </Import>

    “00000A96.dat” and “00000AAA.dat” are removed.

    If this does not work for you, please let me know.

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, August 19, 2013 5:04 AM