Pipe2290
Pipe2290

Reputation: 168

StringWriter.ToString() is breaking xml C#

I have a class array that I'm going to pass to XML to send it later to SQL Server Stored Procedure. The problem is that when I execute StringWriter it adds some scape caracters (=\) that appears in the final XML.

My code is the following:

  public XmlDocument ToXML(object obj_to_xml)
{
    string str_XML;
    XmlDocument xml = new XmlDocument();
    var stringwriter = new System.IO.StringWriter();
    var serializer = new XmlSerializer(obj_to_xml.GetType());
    serializer.Serialize(stringwriter, obj_to_xml);**

    //at this point the format is correct, for example stringwriter contains: 
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfRegistroPrestamo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <RegistroPrestamo>
    <Id>1</Id>
    <Cedula>8-834-789</Cedula>
    <Nombre>Amarillos</Nombre>
    <Apellido>Perez</Apellido>
  </RegistroPrestamo>
  <RegistroPrestamo>
    <Id>0</Id>
    <Cedula />
    <Nombre />
    <Apellido />
  </RegistroPrestamo>
</ArrayOfRegistroPrestamo>

    //the problem comes with this line

    xml.LoadXml(stringwriter.ToString());

    //final XML looks like this: 
<?xml version=\"1.0\" encoding=\"utf-16\"?>
<ArrayOfRegistroPrestamo xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
  <RegistroPrestamo>
    <Id>1</Id>
    <Cedula>8-834-789</Cedula>
    <Nombre>Amarillos</Nombre>
    <Apellido>Perez</Apellido>
  </RegistroPrestamo>
  <RegistroPrestamo>
    <Id>0</Id>
    <Cedula />
    <Nombre />
    <Apellido />
  </RegistroPrestamo>
</ArrayOfRegistroPrestamo>

    return xml;
}

When SQL Server tries to parse the XML Code:

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "P_Save_Procedure";
cmd.Parameters.Add("@Registros", SqlDbType.Xml).Value = xml;

Stored Procedure Text

Create procedure [dbo].[P_Save_Procedure]
(@Registros as xml)
as
begin
    select distinct 'Id' = x.v.value('Id[1]', 'Int')
    from @Registros.nodes('/ArrayOfRegistroPrestamo/RegistroPrestamo') x(v)
end

I got the error:

Mens. 9413, Level 16, State 1, Line 4 XML parsing: line 1, character 37; It expected a string literal

If I call the SP manually with the correct version of the XML:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfRegistroPrestamo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <RegistroPrestamo>
    <Id>1</Id>
    <Cedula>8-834-789</Cedula>
    <Nombre>Amarillos</Nombre>
    <Apellido>Perez</Apellido>
  </RegistroPrestamo>
  <RegistroPrestamo>
    <Id>0</Id>
    <Cedula />
    <Nombre />
    <Apellido />
  </RegistroPrestamo>
</ArrayOfRegistroPrestamo>

The result is the expected.

Already have spend 2 days tryng to find a way to get the correct sintax for the XML and got nothing.

Upvotes: 2

Views: 354

Answers (1)

Timothy Ghanem
Timothy Ghanem

Reputation: 1616

See this:

DoNotEscapeUriAttributes

Upvotes: 2

Related Questions