I have a xml file. As per my requirement I need to update empty tag such as I need to change <xml></xml> to <xml/>. Is it possible to change the tags like that..
Thank you…
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
EDIT: explanation!
@Neil Knight has already provided, in a comment, a link to Wikipedia explaining the concept of regular expressions. The part specific to .NET is available here: .NET Framework Regular Expressions
A starting XML tag can be matched with the following regular expression:
<[^>]+>. The[^>]+part can be read as: all characters that are not “>”, with at least one character (so<>is not matched but<a>is). An ending XML tag can be matched with the same kind of expression:</[^>]+>(note the slash after the first character). So the regular expression<[^>]+></[^>]+>matches empty tags such as<foo></foo>(but be careful, it also matches<foo></bar>which is not valid XML code).What we need now is to isolate the characters between “<” and “>”. For that, we use parenthesis:
<([^>]+)>. This instructs the regular expression engine to capture the matched characters. Each group of parenthesis can be referred later in a replacement operation by the “$x” string (where “x” is a number: “$1” for the first matching parenthesis, “$2” for the second one, etc.).So, with a call to
Regex.Replace(xmlString, "<([^>]+)></[^>]+>", "<$1/>"),<foo></foo>will be replaced by<foo/>(“foo” characters are captured, and “$1” is replaced by them).<foo></bar>will also be replaced by<foo/>.I hope that this explanation is enough for @Felix K. ;o)
(my English is not so good, that’s why I did not provide many details)