innerXML删除后,如何删除C#中的空标签?

时间:2019-04-15 20:37:24

标签: c# xml attributes xmlnode

当用户在DataGrid中选择一行并单击Delete时,我想从文档中删除节点。

我的结果显示,仅内部XML已被删除,但标签仍留在我的文档中。

有什么想法吗?

我也尝试过:

n.ParentNode.ChildNodes.Remove(n);
        public void Delete_Click(object sender, RoutedEventArgs e)
        {
            JobList selected = (JobList)DG.SelectedItem;
            MessageBox.Show("ROW WITH ID : " + selected.Job + " HAS BEEN SELECTED.");
            DG.Items.Remove(selected);
            DeleteXmlNode(@"C:\Users\contract_lshamoon\Desktop\arm\arm\arm\xmldb.xml", "Job", selected.Job);
        }
        private static void DeleteXmlNode(string path, string tagname, string searchconditionAttributevalue)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNodeList nodes = doc.GetElementsByTagName(tagname);
            AddFileSecurity(path, FileSystemRights.ReadData, AccessControlType.Allow);
            XmlNode root = doc.DocumentElement;

            foreach (XmlNode n in root)
            {
                Console.WriteLine(n.Name);
                if (n.Attributes["JobId"].Value == searchconditionAttributevalue)
                {
                    n.RemoveAll();
                }
            }
            doc.Save(path);
        }
<?xml version="1.0" encoding="utf-8"?>
<Jobs>
  <Job>
  </Job>
  <Job JobId="d7bf1b4e-a452-4417-aa72-2ba23c246fc3">
    <JobDate>04/09/2019 2:21 PM</JobDate>
    <File>C:\Users\contract_lshamoon\Desktop\filedeleter\index - Copy.js</File>
    <FilePath>C:\Users\contract_lshamoon\Desktop\filedeleter</FilePath>
    <Extension>js</Extension>
    <Age>1</Age>
    <JobComment>Deleted : 62 files.</JobComment>
  </Job>
  <Job JobId="997dd4cd-b29f-4de4-ad0f-4d4d72a5fe28">
    <JobDate>04/09/2019 2:21 PM</JobDate>
    <File>C:\Users\contract_lshamoon\Desktop\filedeleter\index.js</File>
    <FilePath>C:\Users\contract_lshamoon\Desktop\filedeleter</FilePath>
    <Extension>js</Extension>
    <Age>1</Age>
    <JobComment>Deleted : 55 files.</JobComment>
  </Job>
</Jobs>

2 个答案:

答案 0 :(得分:1)

您可以尝试使用n.ParentNode.RemoveChild(n);代替n.RemoveAll();

您不必遍历XmlNode,而必须遍历XmlNodeList。下面我在Rextester中测试了您的数据,您可以尝试一下:

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;

namespace Rextester
{
public class Program
{
    public static void Main(string[] args)
    {
        String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + 
    "<Jobs>\r\n" + 
    "  <Job>\r\n" + 
    "  </Job>\r\n" + 
    "  <Job JobId=\"d7bf1b4e-a452-4417-aa72-2ba23c246fc3\">\r\n" + 
    "    <JobDate>04/09/2019 2:21 PM</JobDate>\r\n" + 
    "    <File>C:\\Users\\contract_lshamoon\\Desktop\\filedeleter\\index - Copy.js</File>\r\n" + 
    "    <FilePath>C:\\Users\\contract_lshamoon\\Desktop\\filedeleter</FilePath>\r\n" + 
    "    <Extension>js</Extension>\r\n" + 
    "    <Age>1</Age>\r\n" + 
    "    <JobComment>Deleted : 62 files.</JobComment>\r\n" + 
    "  </Job>\r\n" + 
    "  <Job JobId=\"997dd4cd-b29f-4de4-ad0f-4d4d72a5fe28\">\r\n" + 
    "    <JobDate>04/09/2019 2:21 PM</JobDate>\r\n" + 
    "    <File>C:\\Users\\contract_lshamoon\\Desktop\\filedeleter\\index.js</File>\r\n" + 
    "    <FilePath>C:\\Users\\contract_lshamoon\\Desktop\\filedeleter</FilePath>\r\n" + 
    "    <Extension>js</Extension>\r\n" + 
    "    <Age>1</Age>\r\n" + 
    "    <JobComment>Deleted : 55 files.</JobComment>\r\n" + 
    "  </Job>\r\n" + 
    "</Jobs>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(str);
        XmlNodeList nodes = doc.SelectSingleNode("Jobs").SelectNodes("Job");
        for (int i = nodes.Count - 1; i >= 0; i--)
        {
             if (nodes[i].Attributes["JobId"] == null) //this statement removes null tags
             {
                 nodes[i].ParentNode.RemoveChild(nodes[i]);
             } else if(nodes[i].Attributes["JobId"].Value == "d7bf1b4e-a452-4417-aa72-2ba23c246fc3") //this statement removes selected tag.
                nodes[i].ParentNode.RemoveChild(nodes[i]);
        }        
        Console.WriteLine(doc.OuterXml);
    }
  }
}

编辑的DeleteXmlNode方法:

private static void DeleteXmlNode(string path, string tagname, string searchconditionAttributevalue)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(path);
    XmlNodeList nodes = doc.GetElementsByTagName(tagname);
    AddFileSecurity(path, FileSystemRights.ReadData, AccessControlType.Allow);

    XmlNodeList nodes = doc.SelectSingleNode("Jobs").SelectNodes("Job");
    for (int i = nodes.Count - 1; i >= 0; i--)
    {
         if (nodes[i].Attributes["JobId"] == null) //this statement removes null tags
         {
             nodes[i].ParentNode.RemoveChild(nodes[i]);
         } else if(nodes[i].Attributes["JobId"].Value == searchconditionAttributevalue) //this statement removes selected tag.
            nodes[i].ParentNode.RemoveChild(nodes[i]);
    }   

    doc.Save(path);
}

答案 1 :(得分:0)

谢谢大家的回答。 这是为我工作的解决方案: 通过从根中删除子节点来工作。

private static void DeleteXmlNode(string path, string tagname, string searchVal)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNodeList nodes = doc.GetElementsByTagName(tagname);
            XmlNode root = doc.DocumentElement;
            foreach (XmlNode n in root)
            {
                if (n.Attributes["Id"].Value == searchVal)
                {
                    root.RemoveChild(n);
                }
            }
            using (var tw = new StreamWriter(@"~../../../../Log/Log.txt".Trim(), true))
            {
                tw.WriteLine(DateTime.Now + "\t" + WinId + "\t DELETED Job ::: " + searchVal);
            }
            doc.Save(path);
        }