Link to home
Start Free TrialLog in
Avatar of dotsandcoms
dotsandcoms

asked on

deleting record from xml file using linq to xml

Hello experts,

I want to delete the particular record from xml file using linq to xml. The xml file structure is:

<?xml version="1.0" encoding="utf-8"?>
<Inovices>
  <Inovice>
    <InvNum>614</InvNum>
    <tb13AdmHr>13</tb13AdmHr>
    <tb14Type>14</tb14Type>
    <tb15Src>15</tb15Src>
    <tb16Dhr>16</tb16Dhr>
    <tb17Stat>17</tb17Stat>
    <tb18CC>18</tb18CC>
    <tb19CC>19</tb19CC>
    <tb20CC>20</tb20CC>
    <tb21CC>21</tb21CC>
    <tb22CC>22</tb22CC>
    <tb23CC>23</tb23CC>
    <tb24CC>24</tb24CC>
    <tb25CC>25</tb25CC>
    <tb26CC>26</tb26CC>
    <tb27CC>27</tb27CC>
    <tb28CC>28</tb28CC>
    <tb29ACDTST>29</tb29ACDTST>
    <tb31OCodeA>31a</tb31OCodeA>
    <tb31ODateA>3/1/2012 12:00:00 AM</tb31ODateA>
    <tb32OCodeA>32a</tb32OCodeA>
    <tb32ODateA>3/1/2012 12:00:00 AM</tb32ODateA>
    <tb33OCodeA>33a</tb33OCodeA>
    <tb33ODateA>3/1/2012 12:00:00 AM</tb33ODateA>
    <tb34OCodeA>34a</tb34OCodeA>
    <tb34ODateA>3/1/2012 12:00:00 AM</tb34ODateA>
    <tb35OCodeA>35a</tb35OCodeA>
    <tb35FDateA>3/1/2012 12:00:00 AM</tb35FDateA>
    <tb35OTDateA>3/2/2012 12:00:00 AM</tb35OTDateA>
    <tb36OCodeA>36a</tb36OCodeA>
    <tb36FDateA>3/1/2012 12:00:00 AM</tb36FDateA>
    <tb36OTDateA>3/2/2012 12:00:00 AM</tb36OTDateA>
    <tb31OCodeB>31b</tb31OCodeB>
    <tb31ODateB>3/1/2012 12:00:00 AM</tb31ODateB>
    <tb32OCodeB>32b</tb32OCodeB>
    <tb32ODateB>3/1/2012 12:00:00 AM</tb32ODateB>
    <tb33OCodeB>33b</tb33OCodeB>
    <tb33ODateB>3/1/2012 12:00:00 AM</tb33ODateB>
    <tb34OCodeB>34b</tb34OCodeB>
    <tb34ODateB>3/1/2012 12:00:00 AM</tb34ODateB>
    <tb35OCodeB>35b</tb35OCodeB>
    <tb35FDateB>3/1/2012 12:00:00 AM</tb35FDateB>
    <tb35OTDateB>3/2/2012 12:00:00 AM</tb35OTDateB>
    <tb36OCodeB>36b</tb36OCodeB>
    <tb36FDateB>3/1/2012 12:00:00 AM</tb36FDateB>
    <tb36OTDateB>3/2/2012 12:00:00 AM</tb36OTDateB>
    <tb39VCodeA>39a</tb39VCodeA>
    <tb39VAmountA>1</tb39VAmountA>
    <tb40VCodeA>40a</tb40VCodeA>
    <tb40VAmountA>2</tb40VAmountA>
    <tb41VCodeA>41a</tb41VCodeA>
    <tb41VAmountA>3</tb41VAmountA>
    <tb39VCodeB>39b</tb39VCodeB>
    <tb39VAmountB>1</tb39VAmountB>
    <tb40VCodeB>40b</tb40VCodeB>
    <tb40VAmountB>2</tb40VAmountB>
    <tb41VCodeB>41b</tb41VCodeB>
    <tb41VAmountB>3</tb41VAmountB>
    <tb39VCodeC>39c</tb39VCodeC>
    <tb39VAmountC>1</tb39VAmountC>
    <tb40VCodeC>40c</tb40VCodeC>
    <tb40VAmountC>2</tb40VAmountC>
    <tb41VCodeC>41c</tb41VCodeC>
    <tb41VAmountC>3</tb41VAmountC>
    <tb39VCodeD>39d</tb39VCodeD>
    <tb39VAmountD>1</tb39VAmountD>
    <tb40VCodeD>40d</tb40VCodeD>
    <tb40VAmountD>2</tb40VAmountD>
    <tb41VCodeD>41d</tb41VCodeD>
    <tb41VAmountD>3</tb41VAmountD>
  </Inovice>
</Inovices>


i want to delete the record based on InvNum. How can i achieve this using linq to xml in C#?

Any help would greatly be appreciated.

Many thanks
Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

XElement xmlTree = new XElement("Root",
    new XElement("Child1", "child1 content"),
    new XElement("Child2", "child2 content"),
    new XElement("Child3", "child3 content"),
    new XElement("Child4", "child4 content"),
    new XElement("Child5", "child5 content")
);
XElement child3 = xmlTree.Element("Child3");
child3.Remove();
Console.WriteLine(xmlTree);
 
This example produces the following output:

<Root>
  <Child1>child1 content</Child1>
  <Child2>child2 content</Child2>
  <Child4>child4 content</Child4>
  <Child5>child5 content</Child5>
</Root>
ASKER CERTIFIED SOLUTION
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dotsandcoms
dotsandcoms

ASKER

excellent solution