Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

deleting record from xml file using linq to xml

Posted on 2012-03-10
3
Medium Priority
?
254 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

650 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