I have some XML created by a SAS V8 routine that I am de-serialising into an object. For some reason, SAS seems to add whitespace to the start and end of every value.
<ROWSET>
<ROW>
<value1> 1 </value1>
<value2> SOMEVALUE </value2>
<value3 />
</ROW>
</ROWSET>
I thought that maybe I could maybe deserialise from an XmlReader with the setting to ignore whitespace, but it doesn't seem to work (the whitespace remains).
Public Function GetData(FileName As String) As ObjectModel
Using r As Xml.XmlReader = Xml.XmlReader.Create(FileName, New Xml.XmlReaderSettings With {.IgnoreWhitespace = True})
Dim o As New ObjectModel
Dim x As New XmlSerializer(o.GetType)
Return x.Deserialize(r)
End Using
End Function
This answer to a similar question suggests trimming the string while reading it, but how can I achieve the same during deserialization?
I am open to suggestions, including changing the SAS V8 code that creates the XML, but it must be SAS V8 code, not V9.
The SAS code that creates the XML is as follows, I am using xmltype=oracle
as it seems to be the nicest output option for V8.
libname myxml xml "&output..\xmldata.xml" xmltype=oracle;
data myxml.xmldata;
set area.xmldata;
run;
Please feel free to give an answer in c# or vb.
EDIT Although the answer below works, using find and replace just feels wrong to me - I would always prefer to make the change while doing the initial write or the read into .net.
I found a good answer here, and as such this question is probably a duplicate of this question.
My resulting code is as follows:
Public Class SasXmlTextReader
Inherits Xml.XmlTextReader
Public Sub New(stream As IO.Stream)
MyBase.New(stream)
End Sub
Public Overrides Function ReadString() As String
Return MyBase.ReadString().Trim()
End Function
End Class
Public Function GetDefects(FileName As String) As ObjectModel
Using s As New IO.StreamReader(FileName)
Using r As New SasXmlTextReader(s.BaseStream)
Dim df As New ObjectModel
Dim x As New XmlSerializer(df.GetType)
Return x.Deserialize(r)
End Using
End Using
End Function