未从XmlDocument中删除节点

本文关键字:节点 删除 XmlDocument 未从 | 更新日期: 2023-09-27 18:01:06

编辑:我发现了错误。XmlNodeList节点=mydoc。SelectNodes("//dict/dict/dict"(;结构错误。正确的xml模式是"/plist/dict/dict/dict",现在我得到了正确的输出。不过,删除节点仍然不起作用结束编辑

在我的代码中,除了我想删除的节点没有被删除之外,一切似乎都很好。

XmlDocument mydoc = new XmlDocument();
    mydoc.Load(@"C:'Users'boston'Documents'Visual Studio 2010'WebSites'iTunes'iTunes Music Library.xml");
    XmlNodeList nodes = mydoc.SelectNodes("//dict/dict/dict");
    Response.Write(nodes.Count + " nodes found in the xml file <hr>");
    string s1;
    for (int i = 1; i <= nodes.Count -1; i++)
    {
        foreach (XmlElement s in nodes[i])
        {
            s1 = s.InnerText;
            int j = s1.CompareTo("Location");
            if (j == 0)
            {
                s1 = s.NextSibling.InnerText;
                if (s1.Contains("201.mp3"))
                {
                    Response.Write(s1.ToString() + "<br>");
                    nodes[i].ParentNode.RemoveChild(nodes[i]);
                }
            }
        }
        }
    mydoc.Save(@"C:'Users'boston'Documents'output4.xml");

罪魁祸首可能是这个。我做错了什么?

nodes[i].ParentNode.RemoveChild(nodes[i]);

类似的帖子"如何从XmlNodeList中删除XmlNode"已经存在,我应用了该语法,但不起作用。

编辑:

看起来问题就在眼前XmlNodeList节点=mydoc。SelectNodes("//dict/dict/dict"(;

如果我使用//dict//dict-path,我会得到几乎相同的结果。//dict路径也是如此。如果我使用"/dict/dict/",那么我在屏幕打印输出中会得到0个结果。

我正在复制一段xml文件,该文件可能会对问题有所了解。

<dict>
<key>Major Version</key><integer>1</integer>
<key>Minor Version</key><integer>1</integer>
<key>Application Version</key><string>10.3.1</string>
<key>Features</key><integer>5</integer>
<key>Show Content Ratings</key><true/>
<key>Music Folder</key><string>folderpath</string>
<key>Library Persistent ID</key><string>77392150B1B5EE9C</string>
<key>Tracks</key>
<dict>
    <key>1791</key>
    <dict>
        <key>Track ID</key><integer>1791</integer>
        <key>Name</key><string>Deewana</string>
        <key>Artist</key><string>Ali Azmat</string>
        <key>Album</key><string>Social Circus</string>
        <key>Genre</key><string>Other</string>
        <key>Kind</key><string>MPEG audio file</string>
        <key>Size</key><integer>6375424</integer>
        <key>Total Time</key><integer>398288</integer>
        <key>Track Number</key><integer>1</integer>
        <key>Year</key><integer>2005</integer>
        <key>Date Modified</key><date>2005-04-13T19:12:57Z</date>
        <key>Date Added</key><date>2011-06-19T20:14:29Z</date>
        <key>Bit Rate</key><integer>128</integer>
        <key>Sample Rate</key><integer>44100</integer>
        <key>Play Count</key><integer>1</integer>
        <key>Play Date</key><integer>3391853283</integer>
        <key>Play Date UTC</key><date>2011-06-25T17:28:03Z</date>
        <key>Persistent ID</key><string>504630D34E216D84</string>
        <key>Track Type</key><string>File</string>
        <key>Location</key><string>filepath</string>
        <key>File Folder Count</key><integer>5</integer>
        <key>Library Folder Count</key><integer>1</integer>
    </dict>

未从XmlDocument中删除节点

首先,您要删除for/foreach循环中的节点。你不能这样做。

           for (int i = 1; i <= nodes.Count -1; i++)
           {
               foreach (XmlElement s in nodes[i])
               {
                  nodes[i].ParentNode.RemoveChild(nodes[i]); //Cannot remove the element for nodes[i]. 
              }
           }

相反,你可以

       var list = new List<XmlElement>();
       for (int i = 1; i <= nodes.Count -1; i++)
       {
           foreach (XmlElement s in nodes[i])
           {
             list.Add(nodes[i]); 
          }
       }
       foreach(var listTemp in list)
       {
          nodes.remove...(listTemp);
       }

您需要将元素放在临时列表中,然后将其删除到for循环之外。