I have a general question regarding this piece of code that I wrote. This
works fine, however I am confused as to how I am able to pull the sb instance
out at the end? When I create the XmlTextWriter class and pass it in sw
(which in turn sw was created with sb) how is the sb object being maintained
and populated via the writer instance? My initial thought was that only
writer would contain the string....but to my surprise the sb instance
contained the string? how, why? How is sb being built? My goal is to
understand this concept so I can develop similar functionality in my classes.
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter writer = new XmlTextWriter(sw);
writer.Formatting = Formatting.Indented;
writer.Indentation = 5;
writer.WriteStartDocument();
writer.WriteComment("XML Validation Request " + System.DateTime.Now);
writer.WriteStartElement("XmlValidationTicket");
writer.WriteStartElement("Request");
writer.WriteCData(fragment);
writer.WriteEndElement();
writer.WriteElementString("Status", status);
writer.WriteElementString("ErrorMessage", errorMsg);
writer.WriteEndElement();
writer.Close();
return sb.ToString();