-2

I am comparing two XML Strings but want to delete some specific attribute element before comparing them.

I want to remove <orderby> attribute element and all data within these tags

XML 1:

<query> <selecttables> <field> <tablerscd>1014</tablerscd> <tablename> OEM IHS Engines</tablename> </field> <orderby> </orderby> </selecttables> </query> 

XML 2:

<query> <selecttables> <field> <tablerscd>1014</tablerscd> <tablename> OEM IHS Engines</tablename> </field> <orderby> <columnrscd>1228</columnrscd> <columnrscd>1229</columnrscd> </orderby> </selecttables> </query> 

My Question is:

How can i remove <orderby> attribute and all data within these tags before I compare them.

6
  • Your xml is invalid. &lt;selecttables> does not have a closing tag. Commented Jun 28, 2016 at 7:03
  • @PhilB I copied specific portion and and xml validity is not the issue Commented Jun 28, 2016 at 7:05
  • <orderby> is an element, not an attribute. Commented Jun 28, 2016 at 7:05
  • Having invalid xml will significantly restrict your options and if this is truely not xml then you should remove xml references from the question. Commented Jun 28, 2016 at 7:07
  • @PhilB I have corrected the xml, now you will be able to answer my question ? Commented Jun 28, 2016 at 7:11

2 Answers 2

1

Similar to my own question about removing comments you can do this:

// load your Xml string into an XDocument var xml = XDocument.Parse(yourxmlstringhere); foreach (var node in xml.SelectNodes("//orderby").ToList()) { node.ParentNode.RemoveChild(node); } 

Which will find all orderbyelements, navigate to their parent element and delete them from there.

I adopted this answer from Anthony, who pointed out that it will be safer to create a List before removing.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this very trivially using LINQ to XML:

var doc = XDocument.Parse(xml); doc.Descendants("orderby").Remove(); 

See this fiddle for a demo.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.