How to use XmlWriterSettings() when using override void WriteEndElement()?

前端 未结 5 1658
北恋
北恋 2020-12-11 16:15

I am working with a legacy application that does not import abbreviated empty xml elements. For example:

BAD empty:


G

5条回答
  •  自闭症患者
    2020-12-11 16:57

    Another option.

    public class XmlCustomTextWriter : XmlTextWriter
    {
        private TextWriter _tw = null;
    
        public XmlCustomTextWriter(TextWriter sink)
            : base(sink)
        {
            _tw = sink;
            Formatting = Formatting.Indented;
            Indentation = 0;
        }
    
        public void OutputElement(string name, string value)
        {
            WriteStartElement(name);
            string nl = _tw.NewLine;
            _tw.NewLine = "";
            WriteString(value);
            WriteFullEndElement();
            _tw.NewLine = nl;
        }
    }
    

提交回复
热议问题