0

I have the following code which was created after using the telerik code converter to translate a piece of legacy code into C# but it is yielding the error

Error 473 Member 'System.Xml.XmlWriter.Create(System.IO.Stream, System.Xml.XmlWriterSettings)' cannot be accessed with an instance reference; qualify it with a type name instead

public virtual string Serialize()
{
    System.IO.StreamReader streamReader = null;
    System.IO.MemoryStream memoryStream = null;
    try
    {
        memoryStream = new System.IO.MemoryStream();
        System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings();
        xmlWriterSettings.Indent = false;
        System.Xml.XmlWriter xmlWriter = xmlWriter.Create(memoryStream, xmlWriterSettings);
        Serializer.Serialize(xmlWriter, this);
        memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
        streamReader = new System.IO.StreamReader(memoryStream);
        return streamReader.ReadToEnd();
     }
     finally
     {
         if ((((streamReader) != null)))
         {
             streamReader.Dispose();
         }
         if ((((memoryStream) != null)))
         {
             memoryStream.Dispose();
         }
     }
}

The error occurs on the line:

System.Xml.XmlWriter xmlWriter = xmlWriter.Create(memoryStream, xmlWriterSettings);

In VB.Net the code was as follows:

Public Overridable Function Serialize() As String
    Dim streamReader As System.IO.StreamReader = Nothing
    Dim memoryStream As System.IO.MemoryStream = Nothing
    Try
        memoryStream = New System.IO.MemoryStream()
        Dim xmlWriterSettings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings()
        xmlWriterSettings.Indent = False
        Dim xmlWriter As System.Xml.XmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings)
        Serializer.Serialize(xmlWriter, Me)
        memoryStream.Seek(0, System.IO.SeekOrigin.Begin)
        streamReader = New System.IO.StreamReader(memoryStream)
        Return streamReader.ReadToEnd
    Finally
        If (Not (streamReader) Is Nothing) Then
            streamReader.Dispose()
        End If
        If (Not (memoryStream) Is Nothing) Then
            memoryStream.Dispose()
        End If
    End Try
End Function
AGB
  • 2,230
  • 1
  • 14
  • 21
Jay
  • 3,012
  • 14
  • 48
  • 99

2 Answers2

2

This seems kinda invalid in most of the languages I have used so far

type varName = varName.DoSmth();

The confusion of the automatic conversion tool comes from the similarity of the type and the variable name. You need to call Create() as a static method from XmlWriter

System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(memoryStream, xmlWriterSettings);

P.S.: It is never good idea to use tools to generate code.

Royal Bg
  • 6,988
  • 1
  • 18
  • 24
  • Thanks for the explanation Royal I know its not a good means of generating code I just thought it might be handy to understand what is going on by converting it to C# where it is a lot more readable to me – Jay Apr 10 '16 at 19:06
2

The casing is incorrect. Try changing xmlWriter.Create to XmlWriter.Create (with a capital X). I'm not sure why the converter changed that on you. It apparently just got confused because the variable and type names were the same.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105