开发者

populating obj from XML string after editing or removing few elements , in C#

开发者 https://www.devze.com 2023-03-25 02:32 出处:网络
I m having following type o开发者_如何学Gof content in a string variable <root> <head>

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.

0

精彩评论

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