2011-10-17 5 views
0

asp.netを使用しました。第三者のWebサイトから提供されたXMLファイルがあります。最初のメインノードのみが表示されるようにスクラップしておきたい。私の問題は、いずれのノードにも属性がないことです。どうすればそれらを削除できますか?属性がないノードを削除する

以下は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> 
    </class_offer> 
</offers> 

<?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> 

そして、私はそれが、これは以下の、唯一の最初に保つようにしたい(最後の2 "<class_offer>"が削除された、と"<location>"は削除されました)ノード内の属性なしで削除します。もし誰かがそれが偉大になるのを助けることができたら!前もって感謝します。私の頭の上から

答えて

0

XElement root = XElement.Parse(xml); 
XElement firstNode = root.Element("offers").Elements().First(); 
firstNode.Element("location").Remove(); 
foreach(XElement x in root.Element("offers").Elements().Skip(1)) 
    x.Remove(); 
関連する問題