从xml删除元素

时间:2018-10-01 14:26:33

标签: c# xml linq-to-xml

我正在将以下xml的项目映射到gridView asp中:

<?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>

当我单击gridView的删除按钮时,我试图删除特定项目。

这是我的代码:

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"));
    }

但什么也没发生。

我用它代替了Desendant:

xdoc.Elements("grammar")

请您检查一下我是否错过了什么。预先感谢。

1 个答案:

答案 0 :(得分:1)

请尝试以下更改。从列表中删除项目时,您需要从列表末尾开始删除,这样就不会跳过项目。举个例子,如果您有4,5,6并删除了5。6变成5,最后跳过6。您还缺少名称空间。

SELECT *
FROM
    top_lines t
LEFT JOIN
    last_24_topline AS l ON l.member_no = t.member_no AND l.mfg = t.line_no
WHERE
    l.id IS NULL
OR
    (l.id IS NOT NULL AND l.account_no = '32049')
相关问题