DooDoo
DooDoo

Reputation: 13487

How To remove a specific tag from XElement

Please consider this XML:

<Employees>
    <Person>
        <ID>1000</ID>
        <Name>Nima</Name>
        <LName>Agha</LName>
    </Person>
    <Person>
        <ID>1002</ID>
        <Name>Ligha</Name>
        <LName>Ligha</LName>
    </Person>
    <Person>
        <ID>1003</ID>
        <Name>Jigha</Name>
        <LName>Jigha</LName>
    </Person>
</Employees>

I load it in to a Xelement variable.Now How I can remove second Person tag from above XML?

thanks

Upvotes: 0

Views: 1133

Answers (1)

Perfect28
Perfect28

Reputation: 11327

If the only condition is the second node Person, you can use this.

XElement  rootElement = .... // init your rootElement as you want 

rootElement.Elements("Person").Skip(1).Remove() ; 

Upvotes: 2

Related Questions