Render Empty XML Elements as Parent Elements

后端 未结 3 670
悲&欢浪女
悲&欢浪女 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:02

    I extended XmlTextWriter so that I could override the WriteEndElement() method, forcing it to call WriteFullEndElement(). This did the trick.

    Note: for anybody that saw my question update, please ignore. IE was rendering the XML in the shorthand form. As soon as I opened it in Notepad, I realized everything was working fine.

    public class FullEndingXmlTextWriter : XmlTextWriter
    {
        public FullEndingXmlTextWriter(TextWriter w)
            : base(w)
        {
        }
    
        public FullEndingXmlTextWriter(Stream w, Encoding encoding)
            : base(w, encoding)
        {
        }
    
        public FullEndingXmlTextWriter(string fileName, Encoding encoding)
            : base(fileName, encoding)
        {
        }
    
        public override void WriteEndElement()
        {
            this.WriteFullEndElement();
        }
    }

提交回复
热议问题