Hi.

Quite a simple question I guess.

I have this xml file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<tasks>
  <subject>
    <id>123</id>
    <headline>a big headline</headline>
    <content>important stuff</content>
  </subject>
  
<subject>
    <id>222</id>
    <headline>even bigger headline</headline>
    <content>important stuff 2</content>
  </subject>

  <subject>
    <id>333</id>
    <headline>a small one</headline>
    <content>important stuff333</content>
  </subject>

</tasks>

With C# I have loaded this file using Xdocument:

XDocument xmlDoc = XDocument.Load(@"C:\tasks.xml");

Now how do I remove a <subject> element (with all it´s nodes) based on what ID element it has?

And lets say that that the ID is not a sub-element, but an attribute like this:

<subject id="333">
    <headline>a small one</headline>
    <content>important stuff333</content>
  </subject>

then how can i remove that one?

Thanks alot for your help!

Recommended Answers

All 4 Replies

Hi.

Quite a simple question I guess.

I have this xml file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<tasks>
  <subject>
    <id>123</id>
    <headline>a big headline</headline>
    <content>important stuff</content>
  </subject>
  
<subject>
    <id>222</id>
    <headline>even bigger headline</headline>
    <content>important stuff 2</content>
  </subject>

  <subject>
    <id>333</id>
    <headline>a small one</headline>
    <content>important stuff333</content>
  </subject>

</tasks>

With C# I have loaded this file using Xdocument:

XDocument xmlDoc = XDocument.Load(@"C:\tasks.xml");

Now how do I remove a <subject> element (with all it´s nodes) based on what ID element it has?

And lets say that that the ID is not a sub-element, but an attribute like this:

<subject id="333">
    <headline>a small one</headline>
    <content>important stuff333</content>
  </subject>

then how can i remove that one?

Thanks alot for your help!

It would be something like this.

XDocument x = new XDocument();
                        x = XDocument.Load( "whatever.xml" );
                       x.Descendants( "subject" ).Where( p => p.Attribute("id").Value == "333" ).FirstOrDefault().Remove();

You can replace the "333" by a variable.

Thanks alot for your answer!

But it doesnt seem to work.
This is what i have:

public static void RemoveSubject()
{
        XDocument xDoc = XDocument.Load(HttpContext.Current.Server.MapPath(@"App_Data\tasks.xml"));
        xDoc.Descendants("subject").Where(p => p.Element("ID").Value == "3").FirstOrDefault().Remove();
}

But this code does nothing. It just skips the element with the correct <ID> node. Like the criteria arent met.
I´ve also tried your version with the attribute instead of element, but also with no result.

Any ideas? :S

It would be something like this.

XDocument x = new XDocument();
                        x = XDocument.Load( "whatever.xml" );
                       x.Descendants( "subject" ).Where( p => p.Attribute("id").Value == "333" ).FirstOrDefault().Remove();

You can replace the "333" by a variable.

My fault, i forgot to save the xml file afterwards!
So thanks alot for your help! :)

Thanks alot for your answer!

But it doesnt seem to work.
This is what i have:

public static void RemoveSubject()
{
        XDocument xDoc = XDocument.Load(HttpContext.Current.Server.MapPath(@"App_Data\tasks.xml"));
        xDoc.Descendants("subject").Where(p => p.Element("ID").Value == "3").FirstOrDefault().Remove();
}

But this code does nothing. It just skips the element with the correct <ID> node. Like the criteria arent met.
I´ve also tried your version with the attribute instead of element, but also with no result.

Any ideas? :S

the attributes("id") will only work if it looks like this.

<subject id="333">
     <headline>a small one</headline>
      <content>important stuff333</content>
      </subject>

if you use id as an attribute do the following. I tested the following and it works:

string text = "<root><subject id=\"1\"></subject><subject id=\"3\"></subject></root>";

                        XDocument xDoc = XDocument.Load( new StringReader( text ) );
                        xDoc.Descendants( "subject" ).Where( p => p.Attribute( "id" ).Value == "3" ).Remove();

If you use it as a child element do the following:

string text = "<root><subject><id>1</id></subject><subject ><id>3</id></subject></root>";

                        XDocument xDoc = XDocument.Load( new StringReader( text ) );
                        xDoc.Descendants( "subject" ).Where( p => p.Element( "id" ).Value == "3" ).Remove();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.