locked
Filter out the unwanted XML noted RRS feed

  • Question

  • User-1293249277 posted

    I have below XML
    <Hotels>
        <Rooms>
            <name>Standars</name>
            <Type>Deluxe</Type>
            <Rate>123123</Rate>
        </Rooms>
        <Rooms>
            <name>Delux</name>
            <Type>Superior</Type>
            <Rate>453</Rate>
        </Rooms>
        <Rooms>
            <name>Suite</name>
            <Type>Superior</Type>
            <Rate>3242</Rate>
        </Rooms>
    </Hotels>

    I want an output as


    <Hotels>
        <Rooms>
            <name>Standars</name>
        </Rooms>
        <Rooms>
            <name>Delux</name>
        </Rooms>
        <Rooms>
            <name>Suite</name>
        </Rooms>
    </Hotels>

    I simply want to remove the Type and Rate nodes and keep the XML hierarcy same.
    I tried using it with Linq. But I am looking for a single link code.
    Just like we write select * from table and get the desited columns (from XML)
    Please suggest an optimized LINQ query.

    Tuesday, January 1, 2013 8:54 AM

Answers

  • User3866881 posted

    Hello,

    You can just use Descedants nodes and do the nodes,here's an example:

    class Program
        {
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load("XMLFile1.xml");
     
                var deleteNodes = from item in doc.Descendants("Rooms")
                                  let names = item.Elements().Where(e => e.Name != "name")
                                  from ditem in names
                                  select ditem;
     
                deleteNodes.Remove();
                doc.Save("c:\\try.txt");
            }
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 1, 2013 8:56 PM
  • User3866881 posted

    Whats skydrive?

    Hi again,

    SkyDrive means a tool that can let you share software in Microsoft's network with the help of Hotmail.

    And for your problem:

    public class MainTest
        {
            static void Main(string[] args)
            {
                var document = XDocument.Load("XMLFile1.xml");
                List<string> removeNames = new List<string> { "Rate""Amenities""Qty" };
                var removeItems = from item in document.Descendants("Rooms")
                                  let ritems = item.Elements().Where(i => removeNames.IndexOf(i.Name.ToString()) >= 0)
                                  select ritems;
     
                foreach (var item in removeItems)
                {
                    item.Remove();
                }
                document.Save("d:\\try.xml");
            }
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2013 8:47 PM

All replies

  • User3866881 posted

    Hello,

    You can just use Descedants nodes and do the nodes,here's an example:

    class Program
        {
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load("XMLFile1.xml");
     
                var deleteNodes = from item in doc.Descendants("Rooms")
                                  let names = item.Elements().Where(e => e.Name != "name")
                                  from ditem in names
                                  select ditem;
     
                deleteNodes.Remove();
                doc.Save("c:\\try.txt");
            }
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 1, 2013 8:56 PM
  • User-1293249277 posted

    How do I keep two nodes, for example i want to keep Name and Rate both. I tried to manipulate the lambda, seems its not workinh.

      <Rooms>
            <name>Suite</name>
            <Rate>3242</Rate>
        </Rooms>

    Wednesday, January 2, 2013 4:03 AM
  • User3866881 posted

    How do I keep two nodes, for example i want to keep Name and Rate both. I tried to manipulate the lambda, seems its not workinh.

    If you wanna remove "Type" script, try this:

    class Program
        {
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load("XMLFile1.xml");
     
                var deleteNodes = from item in doc.Descendants("Rooms")
                                  let names = item.Elements().Where(e => e.Name == "Type")
                                  from ditem in names
                                  select ditem;
     
                deleteNodes.Remove();
                doc.Save("c:\\try.txt");
            }
        }
    Wednesday, January 2, 2013 4:12 AM
  • User-1293249277 posted

    Dear Decker,

    THanks for your replies, it is working with the given XML. But my actual XML has many nodes. like this.

    I have below ACTUAL XML.
    <Hotels>
        <Rooms>
            <name>Standars</name>
            <Type>Deluxe</Type>
            <Rate>123123</Rate>
        <Amenities>sfds</Amenities>
        <Qty>2</Qty>
        </Rooms>
        <Rooms>
            <name>Delux</name>
            <Type>Superior</Type>
            <Rate>453</Rate>
        <Amenities>234</Amenities>
        <Qty>5</Qty>
        </Rooms>
        <Rooms>
            <name>Suite</name>
            <Type>Superior</Type>
            <Rate>3242</Rate>
        <Amenities>xdg</Amenities>
        <Qty>7</Qty>
        </Rooms>
    </Hotels>

    Using your query i can select any one node (type or name) and either include or exclude from my XML.
    But I want to remove the collection of nodes from my XML. Now using the same above XML. Can we get below type of Result using

    your query?


    <Hotels>
        <Rooms>
            <name>Standars</name>
            <Type>Deluxe</Type>
        </Rooms>
        <Rooms>
            <name>Delux</name>
            <Type>Superior</Type>
        </Rooms>
        <Rooms>
            <name>Suite</name>
            <Type>Superior</Type>
        </Rooms>
    </Hotels>


    I am not able to add AND condition in the LINQ where i give mode then one node to be either removed or be kept in the XML.

    Can you please suggest in this direction

    Wednesday, January 2, 2013 6:00 AM
  • User3866881 posted

    Hi again,

    You can just use Descedants to take out all the "Rooms" tag and then use where statement to do filtering and do to call "Remove".

    Wednesday, January 2, 2013 8:10 PM
  • User-1293249277 posted

    In where condition we can check an elements value, how do I check element's NAME there? That was the first thing i tried.

    Thursday, January 3, 2013 1:15 AM
  • User3866881 posted

    how do I check element's NAME there?

    An example to explain it……plz.

    Thursday, January 3, 2013 1:17 AM
  • User-1293249277 posted


            string TArs = GetTARes();

    XElement xTAHotelSearch = XElement.Parse(TArs);
            var selectedroomslist = from SelectedTAlements in xTAHotelSearch.Descendants("Rooms")
                                    where SelectedTAlements.Element("name").Name.ToString().ToLower().Equals("name")
                                   &&
                                   SelectedTAlements.Element("Type").Name.ToString().ToLower().Equals("type")
                                   select SelectedTAlements;

            selectedroomslist.Remove();
            xTAHotelSearch.Save("c:\\try.txt");


            //I know this wont ever work as I am selecting the Rooms node only, so this node will be remove entirly if any of the
            //WHERE CONDITION is true but I am not sure how do I SELECT MULTIPLE nodes ? I get output as <Hotels/> to which I agree.
        }

        private string GetTARes()
        {
            return "<Hotels><Rooms><name>Standars</name><Type>Deluxe</Type><Rate>123123</Rate><Amenities>sfds</Amenities><Qty>2</Qty></Rooms><Rooms><name>Delux</name><Type>Superior</Type><Rate>453</Rate><Amenities>234</Amenities><Qty>5</Qty></Rooms><Rooms>       <name>Suite</name><Type>Superior</Type><Rate>3242</Rate> <Amenities>xdg</Amenities> <Qty>7</Qty> </Rooms></Hotels>";
        }

    Thursday, January 3, 2013 4:15 AM
  • User3866881 posted

    kavita_khandhadia

    kavita_khandhadia

    Well……Can you:

    1) Create a Console App with your xml string.

    2) Plz use comment strings to tell us what result you wanna fetch.

    Submit to SkyDrive and tell us.

    Reguards!

    Thursday, January 3, 2013 4:46 AM
  • User-1293249277 posted

    As explained in my earlier thread


    I have  XML.
    <Hotels>
        <Rooms>
            <name>Standars</name>
            <Type>Deluxe</Type>
            <Rate>123123</Rate>
        <Amenities>sfds</Amenities>
        <Qty>2</Qty>
        </Rooms>
        <Rooms>
            <name>Delux</name>
            <Type>Superior</Type>
            <Rate>453</Rate>
        <Amenities>234</Amenities>
        <Qty>5</Qty>
        </Rooms>
        <Rooms>
            <name>Suite</name>
            <Type>Superior</Type>
            <Rate>3242</Rate>
        <Amenities>xdg</Amenities>
        <Qty>7</Qty>
        </Rooms>
    </Hotels>

    I want output as using LINQ


    <Hotels>
        <Rooms>
            <name>Standars</name>
            <Type>Deluxe</Type>
        </Rooms>
        <Rooms>
            <name>Delux</name>
            <Type>Superior</Type>
        </Rooms>
        <Rooms>
            <name>Suite</name>
            <Type>Superior</Type>
        </Rooms>
    </Hotels>

    Whats skydrive?

    Thursday, January 3, 2013 4:49 AM
  • User3866881 posted

    Whats skydrive?

    Hi again,

    SkyDrive means a tool that can let you share software in Microsoft's network with the help of Hotmail.

    And for your problem:

    public class MainTest
        {
            static void Main(string[] args)
            {
                var document = XDocument.Load("XMLFile1.xml");
                List<string> removeNames = new List<string> { "Rate""Amenities""Qty" };
                var removeItems = from item in document.Descendants("Rooms")
                                  let ritems = item.Elements().Where(i => removeNames.IndexOf(i.Name.ToString()) >= 0)
                                  select ritems;
     
                foreach (var item in removeItems)
                {
                    item.Remove();
                }
                document.Save("d:\\try.xml");
            }
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 3, 2013 8:47 PM
  • User-1293249277 posted
    Lovely, Decker you are great. Thanks.
    Monday, January 7, 2013 2:48 AM
  • User3866881 posted

    Lovely, Decker you are great. Thanks.

    Laughing

    Monday, January 7, 2013 2:52 AM