Home > Software design >  Strange issue while using XElement to remove a node
Strange issue while using XElement to remove a node

Time:06-01

So I have the following code in which I am using XDocument library to parse my XML and perform associated actions on the XML required.

Now while removing a node(s) under a particular block, I observed that it removes the ending tag of the block. Please refer to the working program here to demonstrate this behavior:

You will see that I am trying to remove the lang tags under the hl1 element. It removes the lang tag successfully but also removes the </hl1> ending tag with it.

Why is this behavior happening?

The other question here is that how I can add ONE lang node under the hl1 element with a new value and remove the multiple ones?

Code:

using System;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Text;
using System.Collections.Generic;
using System.Linq;
                    
public class Program
{
    public static void Main()
    {
        var doc = XDocument.Parse(@"<?xml version='1.0' encoding='utf-8'?>
                            <!--<!DOCTYPE nitf SYSTEM 'nitf-3-4.dtd'>-->
                            <nitf>
                              <head>
                              </head>
                              <body>
                                <body.head>
                                  <hedline>
                                    <hl1 id='Headline1' class='1' style='Headline1' MainHead='true'>
                                      <lang class='3' style='Headline1' font='Patrika15' fontStyle='Bold' size='18'>राजस्थान:</lang>
                                      <lang class='3' style='Headline1' font='Patrika15' fontStyle='Bold' size='18'>ऑनलाइनऑफलाइन रजिस्ट्रेशन</lang>
                                    </hl1>
                                    <hl2 id='Headline2' class='1' style='Headline2' MainHead='false'>
                                      <lang class='3' style='Headline2' font='Patrika15' fontStyle='Bold' size='30'>53.15 लाख बच्चों को टीके लगाने के लिए 3456 केन्द्र</lang>
                                    </hl2>
                                    <hl2 id='Headline2' class='1' style='Headline2' MainHead='false'>
                                      <lang class='3' style='Headline2' font='Patrika15' fontStyle='Bold' size='30'>53.15 लाख बच्चों को टीके लगाने के लिए 3456 केन्द्र 12</lang>
                                    </hl2>
                                  </hedline>
                                  <summary></summary>
                                  <quotes>
                                    <quote></quote>
                                  </quotes>
                                </body.head>
                                <body.content id='Bodytext'>
                                </body.content>
                              </body>
                            </nitf>");

        var element = doc.XPathSelectElement("//hedline");
        int hl1=0;

        foreach (XNode node in element.Nodes())
        {
            if(node.ToString().Contains("<hl1"))
            {
                hl1  ;
                string h1text=string.Empty;
                var x = node.XPathSelectElements("lang");
                int count = x.Count();
                var mylist = x.ToList();
                Console.WriteLine(doc);
                for(int i=0; i<count;i  )
                {
                    mylist[i].Remove();
                }
                Console.WriteLine("----------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("----------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine(doc);     
            }
        }
    }
}

CodePudding user response:

Actually when you remove the lang tags, the hl1 tag converts to 'Self-Closing' tag. It means hl1 tag is an empty element. Anyway try below code to remove children of hl1 tag and create new one.

    public static void Main()
    {
        var doc = XDocument.Parse(@"Your XML content");
        //var element = doc.XPathSelectElement("//hedline");
        //int hl1 = 0;

        foreach (XElement xElement in doc.Descendants("hl1"))
        {
            #region remove lang tags

            var langElements = xElement.XPathSelectElements("lang");
            langElements.Remove();

            #endregion
            #region create new element in this 'hl1' tag

            xElement.Add(CreateElement());

            #endregion
        }   
        Console.WriteLine(doc);
    }
    private static XElement CreateElement()
    {
        XElement xe = new XElement("lang");
        xe.SetAttributeValue("class", 3);
        xe.SetAttributeValue("style", "Headline1");
        xe.SetAttributeValue("font", "Patrika15");
        xe.SetAttributeValue("fontStyle", "Bold");
        xe.SetAttributeValue("size", "18");
        xe.Value = "My Value";
        return xe;
    }
  • Related