编辑:我发现了这个错误。 XmlNodeList nodes = 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]);
存在类似的帖子How to remove an XmlNode from XmlNodeList,我应用了该语法,但无效。
修改
看起来问题就在于此 XmlNodeList节点= mydoc.SelectNodes(“// dict / dict / dict”);
如果我使用// dict // dict路径,我会得到几乎相同的结果。 // 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>
答案 0 :(得分:2)
首先,您尝试删除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循环外删除它。