0

親ノードを削除して実装するつもりですが、再度実行すると例外が発生し、解析エラーのようです

以下は私の元の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;
}
4

1 に答える 1

1

同様に、次のemptyElementsことを行う必要があります。brand

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;
}
于 2013-07-20T15:27:38.437 に答える