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.
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
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");
}
}
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
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;
//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. }
kavita_khand...
Star
9767 Points
1930 Posts
Filter out the unwanted XML noted
Jan 01, 2013 12:54 PM|LINK
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.
I would love to change the world, but they wont give me the source code.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Filter out the unwanted XML noted
Jan 02, 2013 12:56 AM|LINK
Hello,
You can just use Descedants nodes and do the nodes,here's an example:
kavita_khand...
Star
9767 Points
1930 Posts
Re: Filter out the unwanted XML noted
Jan 02, 2013 08:03 AM|LINK
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>
I would love to change the world, but they wont give me the source code.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Filter out the unwanted XML noted
Jan 02, 2013 08:12 AM|LINK
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"); } }kavita_khand...
Star
9767 Points
1930 Posts
Re: Filter out the unwanted XML noted
Jan 02, 2013 10:00 AM|LINK
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
I would love to change the world, but they wont give me the source code.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Filter out the unwanted XML noted
Jan 03, 2013 12:10 AM|LINK
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".
kavita_khand...
Star
9767 Points
1930 Posts
Re: Filter out the unwanted XML noted
Jan 03, 2013 05:15 AM|LINK
In where condition we can check an elements value, how do I check element's NAME there? That was the first thing i tried.
I would love to change the world, but they wont give me the source code.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Filter out the unwanted XML noted
Jan 03, 2013 05:17 AM|LINK
An example to explain it……plz.
kavita_khand...
Star
9767 Points
1930 Posts
Re: Filter out the unwanted XML noted
Jan 03, 2013 08:15 AM|LINK
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>";
}
I would love to change the world, but they wont give me the source code.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Filter out the unwanted XML noted
Jan 03, 2013 08:46 AM|LINK
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!