| |

VerySource

 Forgot password?
 Register
Search
View: 756|Reply: 4

How to delete a node in an XML file with XmlDocument in ASP.NET?

[Copy link]

1

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-3 22:30:01
| Show all posts |Read mode
This is the case, I can delete everything in the node <User> </ User> with the following program, but I cannot delete the <User> </ User> node

    protected void DeleteButton_Click (object sender, EventArgs e)
    {
        XmlDocument Doc = new XmlDocument ();
        Doc.Load (Server.MapPath (".\\db\\dbGuest.xml"));
        XmlNodeList NodeList = Doc.SelectSingleNode ("dbGuest"). ChildNodes;

        foreach (XmlNode xn in NodeList)
        {
            XmlElement xe = (XmlElement) xn;
            XmlNodeList node = xe.GetElementsByTagName ("Name");

            if (node.Count> 0)
            {
                if (node ​​[0] .InnerText == ddlName.SelectedItem.Text)
                {
                    xe.RemoveAll ();
                    break;
                }
            }
            //xn.RemoveAll ();
        }

        Doc.Save (Server.MapPath (".\\db\\dbGuest.xml"));
        DataBind ();
    }

XML file:

<? xml version = "1.0" standalone = "yes"?>
<dbGuest>
  <User>
    <Name> asfew3ere1sfasf </ Name>
  </ User>
  <User>
    <Name> asfew3243232cvd1sfasf </ Name>
  </ User>
</ dbGuest>

For example, I pressed the DeleteButton to delete the second <User> </ User>. The XML file I want is like this:
<? xml version = "1.0" standalone = "yes"?>
<dbGuest>
  <User>
    <Name> asfew3ere1sfasf </ Name>
  </ User>
</ dbGuest>
But what I actually get is this:
<dbGuest>
  <User>
    <Name> asfew3ere1sfasf </ Name>
  </ User>
  <User>
  </ User>
</ dbGuest>

How to solve it?
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-1-3 22:33:01
| Show all posts
string xml = @ "<? xml version =" "1.0" "standalone =" "yes" "?>
<dbGuest>
  <User>
    <Name> asfew3ere1sfasf </ Name>
  </ User>
  <User>
    <Name> asfew3243232cvd1sfasf </ Name>
  </ User>
</ dbGuest> ";

        XmlDocument Doc = new XmlDocument ();
        //Doc.Load(Server.MapPath(".\\db\\dbGuest.xml "));
        Doc.LoadXml (xml);
        XmlNode node = Doc.SelectSingleNode ("// Name [. = 'Asfew3243232cvd1sfasf']");

        if (node! = null)
        {
            node.ParentNode.ParentNode.RemoveChild (node.ParentNode);

        }

        Doc.Save (Response.OutputStream);
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-1-3 22:48:01
| Show all posts
If you write it,

Just change it.
xe.RemoveAll ();
=========> xe.ParentNode.RemoveChild (xe);
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-1-3 22:54:01
| Show all posts
xe represents the User node
xe.RemoveAll () removes all its children and not itself.
Reply

Use magic Report

1

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-1-3 23:18:01
| Show all posts
Thank you for your help, I just won't know soon
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list