简体   繁体   English

节点未从 XmlDocument 中删除

[英]Node is not removed from XmlDocument

Edit: I found the bug.编辑:我发现了这个错误。 XmlNodeList nodes = mydoc.SelectNodes("//dict/dict/dict"); XmlNodeList 节点 = mydoc.SelectNodes("//dict/dict/dict"); has wrong structure.有错误的结构。 The correct xml schema is "/plist/dict/dict/dict" and now i do get the correct output.正确的 xml 架构是“/plist/dict/dict/dict”,现在我得到了正确的 output。 Removing nodes still does not work though End Edit通过结束编辑删除节点仍然不起作用

In my code, everything seems to be fine except the nodes i want to delete are not deleted.在我的代码中,一切似乎都很好,除了我要删除的节点没有被删除。

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

The culprit line might be this.罪魁祸首可能是这个。 What am I doing wrong?我究竟做错了什么?

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

A similar post How to remove an XmlNode from XmlNodeList exist and I applied that syntax but does not work.存在类似的帖子How to remove an XmlNode from XmlNodeList存在,我应用了该语法但不起作用。

Edit:编辑:

Looks like the problem is in the line XmlNodeList nodes = mydoc.SelectNodes("//dict/dict/dict");看起来问题出在 XmlNodeList nodes = mydoc.SelectNodes("//dict/dict/dict");

if I use //dict//dict path, i get almost the same result.如果我使用 //dict//dict 路径,我得到几乎相同的结果。 Same is the case with //dict path. //dict 路径也是如此。 If I use "/dict/dict/" then I get 0 results in the screen print outs.如果我使用“/dict/dict/”,那么我会在屏幕打印输出中得到 0 个结果。

I am copy a piece of xml file which may through some light on the problem.我正在复制一份 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>

First of all you are trying to remove the node which is in for/foreach loop.首先,您试图删除 for/foreach 循环中的节点。 You cannot do this.你不能做这个。

           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]. 
              }
           }

instead you can相反,您可以

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

you need to put the element in temporary List and then remove it outside the for loop.您需要将元素放在临时列表中,然后在 for 循环之外将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM