I am trying to create a class whose end result is do create an XML document. Currently the class consists of nested classes that each build a section of the XML document. What I am hung up on is how I should tie the results of the inner classes for the final output.
Should the outer class pass an instance of XmlTextWriter
to each of the inner classes that build up specific sections or should each innerclass just output a string representation of the XML and the outer class can piece them together?
I have put together a small example to help demonstrate ...
Public Class PatientInfoBuilder
'Instance of XmlTextWriter pass to each class'
Private writer as XmlTextWriter
'This class builds XML containing Patient first name, last name, etc ..'
Private Class PatientInfo
Private mWriter as XmlTextWriter
'Constructor accepts XmlTextWriter instance'
Public Sub New(ByRef writer as XmlTextWriter)
mWriter = writer
End Sub
Public Sub BuildXML()
mWriter.WriteStartElement('FirstName')
...
...
mWriter.WriteEndElement()
End Sub
End Class
'This class builds a list of patient medications'
Private Class PatientMedications
Private mWriter as XmlTextWriter
'Constructor accepts XmlTextWriter instance'
Public Sub New(ByRef writer as XmlTextWriter)
mWriter = writer
End Sub
Public Sub BuildXML()
mWriter.WriteStartElement('Medication')
...
...
mWriter.WriteEndElement()
End Sub
End Class
End Class
The code is not complete but I hope it gives an idea of what I am trying to accomplish. I need to find a way to gather XML sections together to output as a single document. My example is in VB.NET but answers in C# are also welcome. Thanks for any suggestions.
** Please note that I am aware that comments have a trailing apostrophe. I did this so the color coded markup was displayed c开发者_Go百科orrectly in SO.
Any reason you aren't using Data Contracts for this purpose?
It allows you to add attributes to your classes/members which should be serialized, describing how it should be serialized.
When data contracts don't support all the control over the XML output you need, you can also consider using XmlSerializer.
I've seen both done.
I recommend passing around an instance of XMLTextWriter, or better yet, some internal-only structure. Creating an XML writer is similar to writing a compiler, where you'll have variables representing the status of everything at a particular point in your document/text file, for example, the number of brackets that have been opened, any local id references within the current scope, etc. If you try to simply pass back the text/string results, you'll still have to record these variables somewhere -- they'll probably be global variables within your objects.
There is a performance consideration as well. With an object that you pass around, you can add each new bit of text to an Array or List, and then combine them using a String.Join at the end. That's much faster than passing around bits of text, slowly combining each section as you get them, constantly having to ask for and release memory large enough to hold the entire result at any point in the process.
In summary, your first idea is the better one.
Generete a Strongly Typed Dataset. And then work in your code referring to this typed dataset. So you could use a method in the typed dataset to do what you want with XML.
See how to in this link Generating a Strongly Typed DataSet
精彩评论