Render Empty XML Elements as Parent Elements

后端 未结 3 692
悲&欢浪女
悲&欢浪女 2021-01-23 17:39

I have a strange requirement where an application consuming some XML that my application is generating actually needs empty elements to be serialized as parent elements. For exa

3条回答
  •  感动是毒
    2021-01-23 18:07

    You could solve it with a regular expression, making it a two-pass process.

    string xml = "element foo=\"bar\" />"
    
    string pattern = @"<(?\w+)(?\b.*)/>";
    string substitute =  @"<${elem} ${body}>";
    
    Regex regex = new Regex(pattern);
    string goodresult = regex.Replace(xml, substitute);
    

提交回复
热议问题