2011-10-17 91 views
0

我已经使用了asp.net,并且我有一个从第三方网站提供的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> 
    <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>"已被删除)

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

我真的不知道该怎么办在没有节点中的属性的情况下移除。如果有人能帮上忙,那就太棒了!提前致谢。

回答

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();