none
How to remove parent node using XmlDocument class RRS feed

  • Question

  • see this is my xml structure

    <ArrayOfTickerSectionLI xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
    <TickerSectionLI>
    <Ticker>TER</Ticker>
    <Section>Consensus Model</Section>
    </TickerSectionLI>
    
    <TickerSectionLI>
    <Ticker>TER</Ticker>
    <Section>Key Drugs</Section>
    </TickerSectionLI>
    
    </ArrayOfTickerSectionLI>
    tell me how could i remove this node ArrayOfTickerSectionLI using XmlDocument class

    after remove my new xml would be looking like

    <TickerSectionLI>
    <Ticker>TER</Ticker>
    <Section>Consensus Model</Section>
    </TickerSectionLI>
    
    <TickerSectionLI>
    <Ticker>TER</Ticker>
    <Section>Key Drugs</Section>
    </TickerSectionLI>

    thanks

    Friday, July 26, 2019 3:21 PM

Answers

  • Hi Sudip_inn, 

    Thank you for posting here.

    According to your description, it is more suitable to use XDocument to solve your problem.

    Since each XML document has one single root element, I add a root element in the xml.

    Here’s my test.

    XML:


    Code:

                XDocument xdoc = XDocument.Load(path);
    
                var child = xdoc.Descendants("TickerSectionLI");
                XElement Xele = new XElement("ParentNodeOfArray");
                Xele.Add(child);
                XDocument xdoc1 = new XDocument(Xele);
    
                xdoc1.Save(path);

    Result:


    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, July 29, 2019 6:14 AM
    Moderator

All replies

  • Hi Sudip_inn, 

    Thank you for posting here.

    According to your description, it is more suitable to use XDocument to solve your problem.

    Since each XML document has one single root element, I add a root element in the xml.

    Here’s my test.

    XML:


    Code:

                XDocument xdoc = XDocument.Load(path);
    
                var child = xdoc.Descendants("TickerSectionLI");
                XElement Xele = new XElement("ParentNodeOfArray");
                Xele.Add(child);
                XDocument xdoc1 = new XDocument(Xele);
    
                xdoc1.Save(path);

    Result:


    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, July 29, 2019 6:14 AM
    Moderator
  • i asked how to remove parent node not add. please read my post and give answer please. thanks
    Tuesday, July 30, 2019 7:23 AM
  • Hi Sudip_inn, 

    Thanks for your feedback.

    XML documents must contain one root element that is the parent of all other elements.

    So if we remove ArrayOfTickerSectionLI element, there’s no root element in the XML.

    I have updated my code, and I’m assuming that there's a parent node outside.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 31, 2019 5:47 AM
    Moderator