Delete xml elements


Hi guys I'm loading the tag items from the following xml in a dridView:

<?xml version="1.0" encoding="utf-8"?>
<grammar xmlns="" version="1.0" xml:lang="es-MX" mode="voice" tag-format="semantics/1.0" root="grmVoz">
  <rule id="grmVoz" scope="public">
    <ruleref uri="#rule1" />
  <rule id="rule1">
      <item weight="1.0">Ivan Alberto<tag>out+="out1"</tag></item>
      <item weight="1.0">Ivan Alberto2<tag>out+="out2"</tag></item>
      <item weight="1.0">Ivan Alberto3<tag>out+="out3"</tag></item>
      <item weight="1.0">Ivan Alberto4<tag>out+="out4"</tag></item>

and I am trying to delete the item item which I give away from the grid view button:

here's my code:

protected void gvGrammars_RowDeleting(object sender, GridViewDeleteEventArgs e)
 GridViewRow row = (GridViewRow)gvGrammars.Rows[e.RowIndex];
        string valor = row.Cells[0].Text;
        XDocument xdoc = XDocument.Load(Server.MapPath("voiceGrammar.grxml"));
        .Where(x => (string)x.Attribute("id") == "rule1").Elements("one-of").Elements("item").Where(y=> (string)y.Value == valor)

But the xml file is not modified, also probe instead of using Desendants, with Elements, and like nothing happens.

What am I doing wrong, thank you very much in advance.

asked by Ivan Fontalvo 01.10.2018 в 17:59

1 answer


The problem that you are having, is that your XML has an associated namespace, and you are not adding it in the searches, therefore, it does not find anything when searching the nodes.

To fix what is happening to you, add the following line:

XNamespace aw = "";

and in each search of a node, you will have to do:


So, for example, your query would be like this:

    .Where(x => (string)x.Attribute("id") == "rule1").Elements(aw+"one-of").Elements(aw+ "item").Where(y => (string)y.FirstNode.ToString() == valor)
answered by 01.10.2018 / 18:50