Delete xml elements

1

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

<?xml version="1.0" encoding="utf-8"?>
<grammar xmlns="http://www.w3.org/2001/06/grammar" version="1.0" xml:lang="es-MX" mode="voice" tag-format="semantics/1.0" root="grmVoz">
  <rule id="grmVoz" scope="public">
    <ruleref uri="#rule1" />
    <tag>out.cxtag=rules.rule1;out.rule1=rules.rule1;</tag>
  </rule>
  <rule id="rule1">
    <tag>out='';</tag>
    <one-of>
      <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>
    </one-of>
  </rule>
</grammar>

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"));
     xdoc.Descendants("grammar").Elements("rule")
        .Where(x => (string)x.Attribute("id") == "rule1").Elements("one-of").Elements("item").Where(y=> (string)y.Value == valor)
        .Remove();
xdoc.Save(Server.MapPath("voiceGrammar.grxml"));
    }

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
source

1 answer

2

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 = "http://www.w3.org/2001/06/grammar";

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

aw+"nodoabuscar"

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

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