【问题标题】:XML remove child node by attributeXML按属性删除子节点
【发布时间】:2012-07-16 12:54:13
【问题描述】:

我有一个用于播放器设置的 xml 文件。这是我的xml文件

<Player>
 <Volume Value="0,4044944" />
  <Playlist>
   <Song Path="E:\Music\Main\Chipmunk ft. Chris Brown - Champion (Explicit Version).mp3" />
    <Song Path="E:\Music\Main\Usher - More (RedOne Jimmy Joker Remix).mp3" />
 </Playlist>
  <LastPosition LastSong="" Position="" />
 </Player>

我想制作一个按钮来删除具有所选路径的节点。我有变量“路径”来保存目录信息。当我输入代码时:

Doc.Descendants("Song").Where(p => p.Element("Path").Value == path).FirstOrDefault().Remove();

我得到一个 System.NullReferenceException 。你能帮帮我吗?

【问题讨论】:

    标签: c# xml linq


    【解决方案1】:

    应该是p.Attribute,而不是p.Element

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      相关资源
      最近更新 更多