-3

this is my XML

<root>
<CHILD ="1" UID="1">
<GrandChild>1</GrandChild>  
</CHILD>
<CHILD ="2" UID="2">
<GrandChild>2</GrandChild>  
</CHILD>
</root>

how can i remove child 1 from xml

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 2
    [so] is *not* a free code writing service. You are expected to try to **write the code yourself**. After [doing more research](http://meta.stackoverflow.com/questions/261592) if you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a **[mcve]**. I suggest reading [*How do I ask a Good Question*](/help/how-to-ask) and [*Writing the Perfect Question*](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Also, be sure to take the [tour]. – Igor Feb 11 '19 at 18:33
  • 2
    Also that is not valid XML. – Igor Feb 11 '19 at 18:34

1 Answers1

1

Your xml is invalid because of the 'Child ="1"' syntax.

With valid xml you can parse using System.Xml.XmlDocument:

using System.Xml;

Create a new XmlDocument object:

XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object

If you are reading xml from a file, use xmlDoc.Load(string filename):

xmlDoc.Load("yourXMLFile.xml");

If you are reading xml from a string, use xmlDoc.LoadXml(string xml):

xmlDoc.LoadXml(xmlStringVariable);

From there, you can parse through your XML by the tag names and child nodes. Here is a simple example, but hopefully it will give you a start:

XmlNodeList childList = xmlDoc.GetElementsByTagName("CHILD");

var _child = childList[0];

for (int i = 0; i < childList.Count; i++)
{
    // Do work 

    // Loop through child nodes
    for(int c = 0; c < childList[i].ChildNodes.Count; c++)
    {
        // Do something with child nodes
        var _childNode = childList[i].ChildNodes[c].InnerXml;
    }
}
dvo
  • 2,113
  • 1
  • 8
  • 19