Removing whitespace created while Adding elements (when we delete Elements) in XML

StackOverflow https://stackoverflow.com/questions/20214506

  •  05-08-2022
  •  | 
  •  

Вопрос

I am Adding Some 3 elements to Root element using (We are using .Net 2.0)

xnode.AppendChild(parentINode);
xnode.AppendChild(config.CreateTextNode("\r\n"));
//then removeing all added elements using 
xnode.ParentNode.RemoveChild(xnode);

This Will Add a whitespace, I want to remove whitespace that were addedd by adding elements, while removing the childnode.

My original file looks as below:

  <c123:Places State="Mine">
  <!-- Names-places -->
 </c123:Places>

We will Be adding some elements to it :

    <c123:Images State="mine">
     <!-- Names-Places -->
     <Name place=11111>
     <Name place=22222>
     </c123:Places>

Then we will delete elements :then it looks as below

   <c123:Images State="Mine">
   <!-- Names-Images -->


    </c123:Images>

Here we are getting number of white spaces equal to number of elements added, we want to remove/Avoid the white space created after deleting child nodes.

Thanks & Regards,
Channabasappa M

Это было полезно?

Решение

I Did Just Traversing to XML tag then Removed whitespace with checking

    //Removing whitespace created 

   if (xnode.NextSibling != null && xnode.NextSibling.NodeType == XmlNodeType.Whitespace)
      {
            xnode.ParentNode.RemoveChild(xnode.NextSibling);
      }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top