Node is not removed from XmlDocument
Edit: I found the bug. XmlNodeList nodes = 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. 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.
Edit:
Looks like the problem is in the line XmlNodeList nodes = mydoc.SelectNodes("//dict/dict/dict");
if I use //dict//dict path, i get almost the same result. Same is the case with //dict path. If I use "/dict/dict/" then I get 0 results in the screen print outs.
I am copy a piece of xml file which may through some light on the problem.
<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. 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.
精彩评论