Solved

deleting record from xml file using linq to xml

Posted on 2012-03-10
3
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 37707867
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
ID: 37716054
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
ID: 37718525
excellent solution
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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. 
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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