populating obj from XML string after editing or removing few elements , in C#
I m having following type o开发者_如何学Gof content in a string variable
<root>
<head>
</head>
<body>
<head>
</head>
<params>
</params>
</body>
</root>
so either using LINQ or anything else I wish to remove this second head element that is inside body tag. so that resultant string becomes like this .
<root>
<head>
</head>
<body>
<params>
</params>
</body>
</root>
how to do this , is thr any simpler approach rather then string matching or pattern matching approaches.
Thanks in adv guys
Here is a way on how you can implement what you want.
XDocument doc = XDocument.Parse(""); // use Parse when you have a xml string or use XDocument.Load("") if you have a xml file
var element = doc.Descendants("body").Elements("head"); //selects all head elements that are under body element
if (element != null)
element.Remove();
string result = doc.ToString();
Well, you could read the string into an XmlDocument, locate the offending node and delete it, the serialize the document again.
精彩评论