Solved

deleting record from xml file using linq to xml

Posted on 2012-03-10
3
246 Views
Last Modified: 2012-03-13
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
0
Comment
Question by:dotsandcoms
3 Comments
 
LVL 25

Expert Comment

by:apeter
Comment Utility
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>
0
 
LVL 19

Accepted Solution

by:
Shahan Ayyub earned 500 total points
Comment Utility
Hi!

Have a look at this one:

            XDocument doc = XDocument.Load("YourXMLFilePath");
            doc.Descendants("Inovices").Descendants("Inovice").Where(c => c.Element("InvNum").Value == "InvoiceNumberHere").Remove();

Open in new window



e.g.,

XDocument doc = XDocument.Load(@"C:\DataContext.xml");
doc.Descendants("Inovices").Descendants("Inovice").Where(c => c.Element("InvNum").Value == "615").Remove();
0
 

Author Closing Comment

by:dotsandcoms
Comment Utility
excellent solution
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now