开发者

Deleting an XML tag using C#

开发者 https://www.devze.com 2022-12-24 20:01 出处:网络
FIRST EDIT I\'m fetching the Child 1 tag into a DropDownList in my C# form, Plz suggest the best practise code (C#) for deleting a Particular Parent tag & all it\'s child tags in an XML file.

FIRST EDIT

I'm fetching the Child 1 tag into a DropDownList in my C# form, Plz suggest the best practise code (C#) for deleting a Particular Parent tag & all it's child tags in an XML file. Example of my xml file:

    <Parents>
      <Parent>
        <Child 1>开发者_JAVA技巧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
}


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


Here's 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

精彩评论

暂无评论...
验证码 换一张
取 消