El TecnoBaúl de Kiquenet

Kiquenet boring stories

Archive for 23 septiembre 2014

XmlDocument vs XDocument in C#

Posted by kiquenet en 23 septiembre 2014

Attention:

Ensure Encoding ANSI not UTF-8 (view using Notepad++ option)

Xml Namespace (ns)

Using

using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Collections.Generic;


var xml = @"           
<Project xmlns=""
http://schemas.microsoft.com/developer/msbuild/2003"">
  <PropertyGroup>
    <MfaAvisosComoErrores>False</MfaAvisosComoErrores>
    <MfaEntorno>Desarrollo</MfaEntorno>
  </PropertyGroup>
</Project>";

            var xml2 = @"           
<Project>
  <PropertyGroup>
    <MfaAvisosComoErrores>False</MfaAvisosComoErrores>
    <MfaEntorno>Desarrollo</MfaEntorno>
  </PropertyGroup>
</Project>";

XmlDocument

var xDoc1 = new System.Xml.XmlDocument();
xDoc1.Load(f1);

var nodes = xDoc1.GetElementsByTagName("MfaAvisosComoErrores");
Assert.AreNotEqual(0, nodes.Count);
Assert.AreEqual("False", nodes[0].InnerText, "MfaAvisosComoErrores");

XDocument withouth namespace

var doc2 = XDocument.Parse(xml2);
Assert.AreNotEqual(0, doc2.Descendants("MfaAvisosComoErrores").ToList().Count);

XDocument with namespace

XNamespace ns = http://schemas.microsoft.com/developer/msbuild/2003;
var doc1 = XDocument.Parse(xml);
Assert.AreNotEqual(0, doc1.Descendants(ns + "MfaAvisosComoErrores").ToList().Count);
Assert.AreNotEqual(0, doc1.Root.Descendants(ns + "MfaAvisosComoErrores").ToList().Count);

From file

                        var doc = XDocument.Load(f1);
            Assert.AreEqual(0, doc.Descendants("MfaAvisosComoErrores").ToList().Count);

            var MfaAvisosComoErrores = doc.Element(ns + "Project").Element(ns + "PropertyGroup").Element(ns + "MfaAvisosComoErrores").Value;
            Assert.AreEqual("False", MfaAvisosComoErrores, "MfaAvisosComoErrores");
            var MfaEntorno = doc.Element(ns + "Project").Element(ns + "PropertyGroup").Element(ns + "MfaEntorno").Value;
            Assert.AreEqual("Desarrollo", MfaEntorno, "MfaEntorno");

            var query = from property in doc1.Root.Descendants(ns + "PropertyGroup")
                        let MfaAvisosComoErroresValue = (string)property.Element(ns + "MfaAvisosComoErrores")
                        where MfaAvisosComoErroresValue == "False"
                        select new
                        {
                            MfaAvisosComoErrores = MfaAvisosComoErroresValue,
                        };
            Assert.AreNotEqual(0, query.ToList().Count);

References:
http://stackoverflow.com/questions/3460991/selecting-a-xelement-from-a-xdocument
http://stackoverflow.com/questions/566167/query-an-xdocument-for-elements-by-name-at-any-depth
http://stackoverflow.com/questions/15099197/xdocument-to-list-of-object
http://stackoverflow.com/questions/8460464/finding-element-in-xdocument

Anuncios

Posted in .NET | Etiquetado: , , | Leave a Comment »

DateTime .NET and SQL Server

Posted by kiquenet en 19 septiembre 2014

DateTime Extensions

http://extensionmethod.net/csharp/datetime

http://sharpsnippets.wordpress.com/2013/10/08/nifty-extension-methods-for-datetime-in-c/

http://stackoverflow.com/questions/1188224/how-do-i-extend-a-class-with-c-sharp-extension-methods

Date Formatting in WPF Datagrid
http://stackoverflow.com/questions/7978249/date-formatting-in-wpf-datagrid

<tk:DataGridTextColumn Binding="{Binding StartDate, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" />

If you have some custom format for every culture then you better move that logic to Converter or property getter. Otherwise you can look into some more options with custom date formatting

You can do it by setting the AutoGeneratingColumn handler in XAML:

<DataGrid AutoGeneratingColumn="OnAutoGeneratingColumn"  ..etc.. />

And then in behind code do something like this:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(System.DateTime))
        (e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
}


http://stackoverflow.com/questions/14283068/wpf-datagrid-date-column-with-system-custom-short-date-format

Try setting/binding this as your Converter:

[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter
{
    private const string _format = "dd-MM-yy";

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime date = (DateTime)value;

        return date.ToString(_format);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DateTime.ParseExact((string) value, _format, culture);
    }

}

Then set up your XAML like this:

<Window.Resources>
    <wpfDataGridMisc:DateConverter x:Key="dateConverter"/>
</Window.Resources>

<DataGridTextColumn Binding="{Binding Date, Converter={StaticResource dateConverter}}"/


SQL Server Date and Time

http://www.codeproject.com/Articles/566542/Date-and-Time-Data-Types-and-Functions-SQL-Server

Convert Column from Date to Datetime Sql Server

http://stackoverflow.com/questions/8430611/convert-column-from-date-to-datetime-sql-server

ALTER TABLE Tab ALTER COLUMN LastModified datetime2 not null

(I chose datetime2 over datetime, since the former is recommended for all new development work http://msdn.microsoft.com/en-us/library/ms187819.aspx, and since the column is currently date, I know you’re on SQL Server 2008 or later)


SQL Server Management Studio I get the following error.

Saving changes is not permitted. The changes you have made require the following table to be dropped and re-created.

You can’t change the type of a column in place.  You need to create a new column, copy of the values over, and then drop the original column.

SQL Management Studio usually accomplishes this by creating a temporary table with the new column name, copying the values over, dropping the original table with the old column, and then renaming the new temporary table to the new name.  Often it does this without people even realizing it.

However, this can be a very invasive approach, especially if you already have a lot of rows in the table, so you may want to just write a SQL script add the new column to the table, copy the values over, drop the original column, and then use sp_rename to change the new temporary column name back to the original column name.  This is the same idea as what SQL Management Studio is doing, except they are dropping and recreating the whole table, and you are just dropping and recreating the column.

However, if you DO want to let SQL Manangement Studio do it this way, you can turn off that error message.  I believe it was originally added because people did not wantdrop and recreate the table by default.  To turn this message off, go to *Tools->Options-?Designers*, and uncheck the **"Prevent saving changes that require table re-creation"**, then you should be able to save your changes in the designer.

[Prevent saving changes that require table re-creation]


SELECT @@LANGUAGE AS ‘@@LANGUAGE’ 
SELECT @@VERSION

DECLARE @date1 date= ’11-21-38′;

DECLARE @date2 date= ’12-29-33 23:20:51′; — will take only the date 
SELECT @date1 AS ‘date1’, @date2 AS ‘date2’

DECLARE @smalldatetime1 smalldatetime = ‘1955-12-13 12:43:31’; — will round to next minute

DECLARE @smalldatetime2 smalldatetime = ‘1955-12-13 12:43:29’; — will not round to next minute 
SELECT @smalldatetime1 AS ‘@smalldatetime1’, @smalldatetime2 AS ‘@smalldatetime2’; 


DECLARE @datetime1 datetime = ’12-23-35′;

DECLARE @time1 time(4) = ’11:10:05.1234′;

DECLARE @datetime2 datetime = @time1  
DECLARE @datetime3 datetime = ‘1955-12-13 12:43:31’;

SELECT @datetime1 AS ‘datetime1’, @datetime2 AS ‘datetimevar2’  , @datetime3 AS ‘datetimevar3’ 


DECLARE @datetime2_1 datetime2(4) = ’12-13-25 12:32:10.1234′;

DECLARE @datetime2_2 datetime2 = ’12-13-25 11:32:10.1234567′;

DECLARE @datetime2_3 datetime2 = ’12-13-25′;  
 
SELECT @datetime2_1 AS ‘datetime2_1’, @datetime2_2 AS ‘datetime2_2’, @datetime2_3 AS ‘datetime2_3’;



References:
http://stackoverflow.com/questions/3477735/convert-datetime-to-string-formatyyyymmdd

Posted in .NET | Etiquetado: , , , , , , | Leave a Comment »