C#删除XML父节点仍然是空节点

本文关键字:节点 仍然是 父节点 删除 XML | 更新日期: 2023-09-27 18:24:06

我将在删除父节点时实现,但当它再次运行时,出现异常,似乎是解析错误

下面是我的原始xml

<?xml version="1.0" encoding="UTF-8"?>
 <stock>
     <brand name="Samsung">
         <product name="Galaxy S2"/>
         <product name="Galaxy S3"/>
         <product name="Galaxy S4"/>
     </brand>      
     <brand name="iPhone">
         <product name="iPhone 4"/>
         <product name="iPhone 5"/>
     </brand>
 </stock>

我的目标是做到这一点:

<?xml version="1.0" encoding="UTF-8"?>
<stock>
    <brand name="Samsung">
        <product name="Galaxy S2"/>
        <product name="Galaxy S3"/>
        <product name="Galaxy S4"/>
    </brand> 
</stock>

下面是我使用RemoveAll()删除的结果;

<?xml version="1.0" encoding="UTF-8"?>
<stock>
   <brand name="Samsung">
      <product name="Galaxy S2"/>
      <product name="Galaxy S3"/>
      <product name="Galaxy S4"/>
  </brand>
  <brand/>
</stock>

下面是我的代码

public bool deleteBrand(string brand)
{
    bool result = false;
    try
    {
        List<string> existingBrandName = getBrand();
        if (existingBrandName.Contains(brand))
        {
            XDocument productList = load();
            var query = from positions in productList.Descendants("brand")
                        where (string)positions.Attribute("name").Value == brand
                        select positions;
            XElement selectedBrand = query.ElementAt(0);
            selectedBrand.RemoveAll();

            var emptyElements = from element in productList.Descendants("stock")
                                where element.IsEmpty
                                select element;
            while (emptyElements.Any())
                emptyElements.Remove();
            productList.Save(path);
            result = true;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

    return result;
}

C#删除XML父节点仍然是空节点

您还需要用brandemptyElements的事情:

public bool deleteBrand(string brand)
{
    bool result = false;
    try
    {
        List<string> existingBrandName = getBrand();
        if (existingBrandName.Contains(brand))
        {
            XDocument productList = load();
            var query = from positions in productList.Descendants("brand")
                        where (string)positions.Attribute("name").Value == brand
                        select positions;
            XElement selectedBrand = query.ElementAt(0);
            selectedBrand.RemoveAll();
            var toCheck = productList.Descendants("stock")
                                     .Concat(productList.Descendants("brand"));
            var emptyElements = from element in toCheck
                                where element.IsEmpty
                                select element;
            while (emptyElements.Any())
                emptyElements.Remove();
            productList.Save(path);
            result = true;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    return result;
}