Hi, I've got an xml file provided from a third party website. I want it to be scraped so it only display the first main node. My problem is there is no attribute on any of the node. How can I manage to remove them?
The following is the xml.
<?xml version="1.0"?>
<offers>
<class_offer>
<name><![CDATA[Learn to surf and save 52% at Muriwai Surf School]]></name>
<url>http://domain.co.nz/wai-surf-school-just-29</url>
<location>Auckland</location>
</class_offer>
<class_offer>
<name><![CDATA[$35 for a 30 minute luxury Slipper Bath experience for TWO]]></name>
<url>http://domain.co.nz/uxury-slipper-bath-experience-for-two</url>
<location>Auckland</location>
</class_offer>
<class_offer>
<name><![CDATA[Save 52% at Te Aroha Mineral Spas]]></name>
<url>http://domain.co.nz/rience-for-two-PLUS-massage</url>
<location>Auckland</location>
</class_offer>
</offers>
And I want it to be this below,only keeps the first <class_offer> (the last 2 "<class_offer>" has been removed, and "<location>" has been removed)
<?xml version="1.0"?>
<offers>
<class_offer>
<name><![CDATA[Learn to surf and save 52% at Muriwai Surf School]]></name>
<url>http://domain.co.nz/wai-surf-school-just-29</url>
</class_offer>
</offers>
I really have no idea what to do to remove without the attribute in the node. Thanks in advance.
You should use XDocument to loop any elements here, and then use ToList<T> the extended method to remove the second and the third element by referring "Remove()" method. Just like this:
XDocument doc = XDocument.Load("xxx.xml");
var result = (from e in doc.Descedants("class-offer")
select e).ToList();
Member
25 Points
125 Posts
remove a node without attribute
Oct 16, 2011 09:35 PM|raywang|LINK
Hi, I've got an xml file provided from a third party website. I want it to be scraped so it only display the first main node. My problem is there is no attribute on any of the node. How can I manage to remove them?
The following is the xml.
And I want it to be this below,only keeps the first <class_offer> (the last 2 "<class_offer>" has been removed, and "<location>" has been removed)
I really have no idea what to do to remove without the attribute in the node. Thanks in advance.
All-Star
94120 Points
18123 Posts
Re: remove a node without attribute
Oct 18, 2011 09:29 PM|Decker Dong - MSFT|LINK
Hello:)
You should use XDocument to loop any elements here, and then use ToList<T> the extended method to remove the second and the third element by referring "Remove()" method. Just like this:
XDocument doc = XDocument.Load("xxx.xml");
var result = (from e in doc.Descedants("class-offer")
select e).ToList();
result[1].Remove();
result[2].Remove();
doc.Save("C:\\try.xml");