Как удалить пустые теги в C # после удаления innerXML? - PullRequest
0 голосов
/ 15 апреля 2019

Я хочу удалить узлы из моего документа, когда пользователь выбирает строку в моей DataGrid и нажимает кнопку Удалить.

Мой результат показывает, что был удален только внутренний 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>

1 Ответ

1 голос
/ 15 апреля 2019

Вы можете попробовать 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);
}
...