Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to remove node from string formatted xml using c#

how to remove node from string formatted xml using c#

Scheduled Pinned Locked Moved C#
csharpxmltutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    michael nabil
    wrote on last edited by
    #1

    I have an string parameter with xml content in it. Basically the string have an XML inside.

    7-1623419127
    2021-06-11T06:45:27-08:00
    2021-06-18T10:26:37-08:00
    1624037197
    3
    E0000-1624055316
    *michael
    470000-1623418914
    Accounts Receivable
    2021-06-11
    1
    *michael
    false
    false
    2021-06-11
    2021-06-11
    1200.00
    10000-1623418832
    Vat 14%
    14.00
    168.00
    0.00
    1368.00
    false
    true
    10000-1623418756
    Tax
    9-1623419127
    30000-1623418915
    *م1
    صنف رقم 1

    10
    30.00
    300.00
    10000-1623418756
    Tax

    12-1623419127
    40000-1623418915
    *م2
    مواصفات ملابس 2
    15
    60.00
    900.00
    10000-1623418756
    Tax

    13-1623419127
    14-1623419127
    60000-1624037191
    total
    1200.00

    i want to Delete

    and

    with out delete

    *م1

    L Richard DeemingR 2 Replies Last reply
    0
    • M michael nabil

      I have an string parameter with xml content in it. Basically the string have an XML inside.

      7-1623419127
      2021-06-11T06:45:27-08:00
      2021-06-18T10:26:37-08:00
      1624037197
      3
      E0000-1624055316
      *michael
      470000-1623418914
      Accounts Receivable
      2021-06-11
      1
      *michael
      false
      false
      2021-06-11
      2021-06-11
      1200.00
      10000-1623418832
      Vat 14%
      14.00
      168.00
      0.00
      1368.00
      false
      true
      10000-1623418756
      Tax
      9-1623419127
      30000-1623418915
      *م1
      صنف رقم 1

      10
      30.00
      300.00
      10000-1623418756
      Tax

      12-1623419127
      40000-1623418915
      *م2
      مواصفات ملابس 2
      15
      60.00
      900.00
      10000-1623418756
      Tax

      13-1623419127
      14-1623419127
      60000-1624037191
      total
      1200.00

      i want to Delete

      and

      with out delete

      *م1

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Use the classes in the System.Xml Namespace | Microsoft Docs[^].

      1 Reply Last reply
      0
      • M michael nabil

        I have an string parameter with xml content in it. Basically the string have an XML inside.

        7-1623419127
        2021-06-11T06:45:27-08:00
        2021-06-18T10:26:37-08:00
        1624037197
        3
        E0000-1624055316
        *michael
        470000-1623418914
        Accounts Receivable
        2021-06-11
        1
        *michael
        false
        false
        2021-06-11
        2021-06-11
        1200.00
        10000-1623418832
        Vat 14%
        14.00
        168.00
        0.00
        1368.00
        false
        true
        10000-1623418756
        Tax
        9-1623419127
        30000-1623418915
        *م1
        صنف رقم 1

        10
        30.00
        300.00
        10000-1623418756
        Tax

        12-1623419127
        40000-1623418915
        *م2
        مواصفات ملابس 2
        15
        60.00
        900.00
        10000-1623418756
        Tax

        13-1623419127
        14-1623419127
        60000-1624037191
        total
        1200.00

        i want to Delete

        and

        with out delete

        *م1

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        The simplest solution would be to use LINQ to XML: Overview - LINQ to XML | Microsoft Docs[^]

        string sourceXml = @"...";
        XDocument document = XDocument.Parse(sourceXml);

        List<XElement> elementsToRemove = document.Descendants("ListID").ToList();
        foreach (XElement element in elementsToRemove)
        {
        element.Remove();
        }

        elementsToRemove = document.Descendants("ItemRef").ToList();
        foreach (XElement element in elementsToRemove)
        {
        element.Remove();
        }

        string resultXml = document.ToString();


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        M 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          The simplest solution would be to use LINQ to XML: Overview - LINQ to XML | Microsoft Docs[^]

          string sourceXml = @"...";
          XDocument document = XDocument.Parse(sourceXml);

          List<XElement> elementsToRemove = document.Descendants("ListID").ToList();
          foreach (XElement element in elementsToRemove)
          {
          element.Remove();
          }

          elementsToRemove = document.Descendants("ItemRef").ToList();
          foreach (XElement element in elementsToRemove)
          {
          element.Remove();
          }

          string resultXml = document.ToString();


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          michael nabil
          wrote on last edited by
          #4

          thanks for your response this code remove all sub nodes in

          ItemRef

          but i need to keep the sub node

          *م1

          thanks

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups