none
C# How to delete element in XML

    Question

  • Hello,
    I have this XML file players.xml
    <players>
      <player>
        <name>administrator</name>
        <game>Cube</game>
        <na_tahu>true</na_tahu>
        <score>45</score>
        <partner></partner>
        <last_move></last_move>
      </player>
      <player>
        <name>User2</name>
        <game>Cube</game>
        <na_tahu>true</na_tahu>
        <score>16</score>
        <partner></partner>
        <last_move></last_move>
      </player>
    </players>
    And I need to delete this whole element
    <player> 
        <name>User2</name> 
        <game>Cube</game> 
        <na_tahu>true</na_tahu> 
        <score>16</score> 
        <partner></partner
        <last_move></last_move
      </player> 
    I can identify this element according to child elements <name> and <game>. So if <name>.InnerText="User2" & <game>.InnerText="Cube" then delete <player>...</player>

    But I don´t know how to write code in C# for finding that right element player. Could somebody please help me how to delete this element?
    Saturday, November 01, 2008 3:23 PM

Answers

  • XmlNode node = doc.SelectSingleNode("/players/player[name='User2']");  
    node.ParentNode.RemoveChild(node);  
     

     

    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 3:36 PM
  • Czechtim said:

    Thank you, but User2 can play some different games so I need to specify game as well to delete right element. Can I somehow join two conditions "/players/player[name='User2']" && "/players/player[game='Cube']"?

    Why would you want to do it? Cube does not seem to be a good identifier. It is present as an XmlNode InnerText in two different nodes. If the Cube were unique then you will have to iterate thru sub nodes and identify it.


    AlexB
    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 3:59 PM
  • > Can I somehow join two conditions

    XmlNode node = doc.SelectSingleNode("/players/player[name='User2' and game='Cube']"); 

    The expression is an XPath expression: http://www.w3schools.com/xpath/default.asp
    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 4:08 PM
  • I solved it


    String aaa = "/players/player[name='"+user+"' and game='"+game+"']"; 
    XmlNode node = docc.SelectSingleNode(aaa); 
    Thank you very much everybody for your answers
    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 4:38 PM
  • Note that the version XPath implemented in .NET does not support escape sequences, so you should validate that your strings do not contain undesirable characters such as quotes.

                string name = "User2";  
                string game = "Cube";  
                XmlNode node = doc.SelectSingleNode(String.Format("/players/player[name='{0}' and game='{1}']", name, game));  
                node.ParentNode.RemoveChild(node); 
    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 4:43 PM

All replies

  • XmlNode node = doc.SelectSingleNode("/players/player[name='User2']");  
    node.ParentNode.RemoveChild(node);  
     

     

    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 3:36 PM
  • Thank you, but User2 can play some different games so I need to specify game as well to delete right element. Can I somehow join two conditions "/players/player[name='User2']" && "/players/player[game='Cube']"?
    Saturday, November 01, 2008 3:40 PM
  • Czechtim said:

    Thank you, but User2 can play some different games so I need to specify game as well to delete right element. Can I somehow join two conditions "/players/player[name='User2']" && "/players/player[game='Cube']"?

    Why would you want to do it? Cube does not seem to be a good identifier. It is present as an XmlNode InnerText in two different nodes. If the Cube were unique then you will have to iterate thru sub nodes and identify it.


    AlexB
    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 3:59 PM
  • > Can I somehow join two conditions

    XmlNode node = doc.SelectSingleNode("/players/player[name='User2' and game='Cube']"); 

    The expression is an XPath expression: http://www.w3schools.com/xpath/default.asp
    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 4:08 PM
  • I´m doing such "game server" when various users can play various games and when someone finishes his game, then I need to erase that record from XML. So I identify player according to name of game (<game>) and name of player (<name>).

    And second problem, I didn´t mention yet. I have name of player and name of game in two variables (String player="User2";...). Can I somehow in "/players/player[name='User2']" instead of text 'User2' insert variable player?
    Saturday, November 01, 2008 4:18 PM
  • I solved it


    String aaa = "/players/player[name='"+user+"' and game='"+game+"']"; 
    XmlNode node = docc.SelectSingleNode(aaa); 
    Thank you very much everybody for your answers
    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 4:38 PM
  • Note that the version XPath implemented in .NET does not support escape sequences, so you should validate that your strings do not contain undesirable characters such as quotes.

                string name = "User2";  
                string game = "Cube";  
                XmlNode node = doc.SelectSingleNode(String.Format("/players/player[name='{0}' and game='{1}']", name, game));  
                node.ParentNode.RemoveChild(node); 
    • Marked as answer by Harry Zhu Wednesday, November 05, 2008 2:59 AM
    Saturday, November 01, 2008 4:43 PM
  • OK thank you very much
    Saturday, November 01, 2008 4:55 PM
  • what is doc in this

     

     

    XmlNode node = doc.SelectSingleNode("/players/player[name='User2']");  
    node.ParentNode.RemoveChild(node); 
    Thursday, December 29, 2011 8:11 PM
  • doc is the XmlDocument that you opened. I tend to wrap my doc.Load statement in a try-except statement, because the Load method will raise an exception if it cannot load the xml document for some reason.

                            doc = newXmlDocument();                         try                         {                             doc.Load("myXMLFile.xml");


    Alastair

    Thursday, May 24, 2012 9:10 AM