Removing an XML tag using C #

FIRST EDIT
I retrieve the Child 1 tag in DropDownList in my C # form, Plz offers a code of best practice (C #) to remove the Parent Parent tag and all its child tags in the XML file. An example of my xml file:

    <Parents>
      <Parent>
        <Child 1>Something</Child 1>
        <Child 2>Something</Child 2>
        <Child 3>Something</Child 3>
        <Child 4>Something</Child 4>
      </Parent>
      <Parent>
        <Child 1>Something 1</Child 1>
        <Child 2>Something 1</Child 2>
        <Child 3>Something 1</Child 3>
        <Child 4>Something 1</Child 4>
      </Parent>
    </Parents>

I mean something like:

for (int i=0; i<[Length of xml doc]; i++)
{
  if (Child 1 == ComboBox1.Text && Child 2 == richTextBox1.Text)
    // Delete <Parent> tag of that Child 1
}
+3
source share
2 answers

You might want to learn about XmlDocument and XmlNode and see what you can do with them. See here , and there are also good examples from msdn here .

+1
source

Here is a small example. Hope this helps !.

XmlDocument doc = new XmlDocument(); 
doc.Load("myXmlFile.xml");

foreach (XmlNode node in doc.ChildNodes)
{
    if (<node.value matches your criteria>)
    {
        doc.RemoveChild(node);
    }

}
0
source

Source: https://habr.com/ru/post/1738474/


All Articles