14 Replies - 2304 Views - Last Post: 11 March 2011 - 03:33 AM Rate Topic: -----

#1 Mindee   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 08-February 11

Removing an element from xml file.

Posted 10 March 2011 - 04:58 AM

Hello. Can someone write example, how to remove element:
<Mindee uid="1" />


From xml file:

<?xml version="1.0" encoding="utf-8"?>
<player premdaysleft="1" name="Wdemon" pass="apacergp" maxhp="100" hp="100" maxmana="100" mana="100" experience="12" level="1" gold="4545" vocation="2" sex="1">
  <access level="0" />
  <temple x="2000" y="2000" z="7" />
  <spawn x="2028" y="1859" z="7" />
  <number val="123456789" country="131577" />
  <inventory slot1="77" slot2="11" slot3="11" slot4="11" slot5="0" slot6="0" slot7="0" slot8="0" slot9="0" slot10="0" slot11="0" slot12="0" slot13="0" slot14="0" slot15="0" slot16="0" />
  <Mindee uid="1" />
</player>


I tried in many ways, but got errors. :sadlike:

Is This A Good Question/Topic? 1
  • +

Replies To: Removing an element from xml file.

#2 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Removing an element from xml file.

Posted 10 March 2011 - 05:05 AM

Which ways did you try?
Was This Post Helpful? 0
  • +
  • -

#3 Mindee   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 08-February 11

Re: Removing an element from xml file.

Posted 10 March 2011 - 05:11 AM

						XmlNode t = xDoc2.SelectSingleNode("player/Mindee/");
						t.ParentNode.RemoveChild(t);

This post has been edited by Mindee: 10 March 2011 - 05:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Removing an element from xml file.

Posted 10 March 2011 - 05:18 AM

Hahahaha. :D I recognize this configuration file! Holy moses...this brings me back. :D

This is for your Tibia OT server right?! Wow! It's been so long. Sorry, didn't mean to go off-topic but this is really taking me down memory lane. ;)
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: Removing an element from xml file.

Posted 10 March 2011 - 05:18 AM

I know next to nothing about doing this, but looking at the two methods on MSDN for 10 seconds shows you aren't using the SelectSingleNode method properly.

MSDN example said:

//Select the book node with the matching attribute value.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr);


Did you even try to look up how to use these methods?
http://msdn.microsof...ode(VS.71).aspx
Was This Post Helpful? 2
  • +
  • -

#6 Mindee   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 08-February 11

Re: Removing an element from xml file.

Posted 10 March 2011 - 05:50 AM

It's not Tibia. It's TibiaME :)

View PosttlhIn`toq, on 10 March 2011 - 05:18 AM, said:

I know next to nothing about doing this, but looking at the two methods on MSDN for 10 seconds shows you aren't using the SelectSingleNode method properly.

MSDN example said:

//Select the book node with the matching attribute value.
      XmlNode book;
      XmlElement root = doc.DocumentElement;
      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr);


Did you even try to look up how to use these methods?
http://msdn.microsof...ode(VS.71).aspx


Thanks. :)

This post has been edited by Mindee: 10 March 2011 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#7 Mindee   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 08-February 11

Re: Removing an element from xml file.

Posted 10 March 2011 - 07:48 AM

Sorry for so many questions, but I got one more idea and I can't realize it.

How to remove all nodes with specific attribute name?

For example, I have:

<Mindaugas name="xxx" />
<Rimas name="xxx" />
<Auguste name="xxx" />




I want to remove all nodes which have attribute name "name=".
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: Removing an element from xml file.

Posted 10 March 2011 - 07:55 AM

Please re-read post #5.
It provides a link to the MSDN page for selecting nodes including the selection of nodes by a specific attribute.

The example pasted in even does this.

I don't know if there is a call that will take them all out at once, but in a worst case you can just loop through the nodes and delete them using the SelectSingleNode method you've already used.
Was This Post Helpful? 2
  • +
  • -

#9 Mindee   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 08-February 11

Re: Removing an element from xml file.

Posted 10 March 2011 - 08:26 AM

It's hard for me as I'm newbie, but thanks I will try! :)
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Removing an element from xml file.

Posted 10 March 2011 - 10:44 AM

To remove all the nodes which have an attribute called name.

Spoiler

Was This Post Helpful? 1
  • +
  • -

#11 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Removing an element from xml file.

Posted 10 March 2011 - 01:14 PM

For future reference, the C# version of AdamSpeight2008's code is:

Spoiler


where 'x' is still the XDocument encapsulating the .xml document (and 'xx' is the current element wrapped in a XElement object).

This post has been edited by [email protected]: 10 March 2011 - 01:29 PM

Was This Post Helpful? 2
  • +
  • -

#12 Mindee   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 08-February 11

Re: Removing an element from xml file.

Posted 11 March 2011 - 01:27 AM

Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#13 Mindee   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 08-February 11

Re: Removing an element from xml file.

Posted 11 March 2011 - 02:36 AM

I got error:

Quote

'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' could be found (are you missing a using directive or an assembly reference?)

Was This Post Helpful? 0
  • +
  • -

#14 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Removing an element from xml file.

Posted 11 March 2011 - 02:47 AM

Make sure you have this using statement:

using System.Linq;


EDIT: While we're on the subject, make sure you have this one too:

using System.Xml.Linq;


:)

This post has been edited by [email protected]: 11 March 2011 - 02:50 AM

Was This Post Helpful? 2
  • +
  • -

#15 Mindee   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 08-February 11

Re: Removing an element from xml file.

Posted 11 March 2011 - 03:33 AM

View Post[email protected], on 11 March 2011 - 02:47 AM, said:

Make sure you have this using statement:

using System.Linq;


EDIT: While we're on the subject, make sure you have this one too:

using System.Xml.Linq;


:)


Forgot to add System.Linq. Thanks. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1