Solved

deleting record from xml file using linq to xml

Posted on 2012-03-10
3
247 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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, …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

939 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

9 Experts available now in Live!

Get 1:1 Help Now