Mostrando entradas con la etiqueta xml. Mostrar todas las entradas
Mostrando entradas con la etiqueta xml. Mostrar todas las entradas

Formato de fechas al exportar un dataset a XML

Cuando se exporta un dataset a XML, las fechas se formatean según la timezone del equipo donde se está ejecutando la exportación.

Ej, si la fecha es
     2003-04-06T00:00:00

cuando se exporta a XML puede quedar así
     2003-04-06T00:00:00-03:00

En este caso le agregó el GMT -3

Una solución posible es cambiar el DateTimeMode del dataset a Unspecified.
De esta forma, no se incluirá el offset (el -3) en el archivo XML.


System.Data.DataSet data = new DataSet();

// ...

// Iterate the columns and set DataColumn.DateTimeMode
// to DataSetDateTime.Unspecified to remove offset
// when serialized.
foreach (DataTable table in data.Tables)
{
    foreach (DataColumn item in table.Columns)
    {
        // Switching from UnspecifiedLocal to Unspecified
        // is allowed even after the DataSet has rows.
        if (item.DataType == typeof(DateTime) &&
            item.DateTimeMode == DataSetDateTime.UnspecifiedLocal)
        {
            item.DateTimeMode = DataSetDateTime.Unspecified;
        }
    }

}

Fuente: http://blog.scosby.com/post/2013/03/01/Serializing-A-DataSet-With-DateTime-Columns.aspx

Acceder a elementos de XML con XPath

IMPORTANTE:
Agregar este using:

using System.Xml.XPath;

XDocument xmlDoc;
xmlDoc = XDocument.Load("Data.xml");


/* coleccion de elementos dentro de un determinado path */

var destinos = xmlDoc.XPathSelectElements("publicador/configuracion/destinos/web");
foreach (var dest in destinos.Elements("destino"))
{
   Console.WriteLine(dest.Value);
}



/* coleccion de elementos cuyo valor contiene un texto específico */

var x = xmlDoc.XPathSelectElement("publicador/aplicaciones/aplicacion/nombre[text()='abc']");



Ejecutar un stored procedure con XML como parametro en Sql Server



Tabla

USE [base]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Empresa](
[Id] [numeric](18, 0) NOT NULL,
[Nombre] [varchar](50) NULL,
[Direccion] [varchar](50) NULL,
 CONSTRAINT [PK_Empresa] PRIMARY KEY CLUSTERED 
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


Stored procedure

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE Prueba
@DatosXml XML
AS
BEGIN
SET NOCOUNT ON;

insert into Empresa (Id, Nombre, Direccion)
SELECT
  Pers.value('(Id)[1]', 'int') as 'Id',
  Pers.value('(Nombre)[1]', 'Varchar(50)') as 'Nombre',
  Pers.value('(Direccion)[1]', 'varchar(50)') as 'Direccion'
FROM
  @DatosXml.nodes('/Empresa/Persona') as EMP(Pers)
END
GO


Ejecucion

exec Prueba @DatosXml = '<Empresa> <Persona> <Id>1</Id> <Nombre>Pedro</Nombre> <Direccion>Dir 1</Direccion> </Persona> <Persona> <Id>2</Id> <Nombre>Juan</Nombre> <Direccion>Dir 2</Direccion> </Persona> </Empresa>'