Skip to content

Instantly share code, notes, and snippets.

@k94ll13nn3
Last active October 7, 2020 07:46
Show Gist options
  • Save k94ll13nn3/6059c4a55a15d07e1edd3c3cb6cfee5c to your computer and use it in GitHub Desktop.
Save k94ll13nn3/6059c4a55a15d07e1edd3c3cb6cfee5c to your computer and use it in GitHub Desktop.
Class snippets
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
namespace Mon
{
/// <summary>
/// This class adds the ability to refresh the list when any property of
/// the objects changes in the list which implements the INotifyPropertyChanged.
/// </summary>
/// <typeparam name="T">The type of elements in the collection.</typeparam>
public class ItemsChangeObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
/// <inheritdoc/>
protected override void ClearItems()
{
UnregisterPropertyChanged(this);
base.ClearItems();
}
/// <inheritdoc/>
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
RegisterPropertyChanged(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
UnregisterPropertyChanged(e.OldItems);
break;
case NotifyCollectionChangedAction.Replace:
UnregisterPropertyChanged(e.OldItems);
RegisterPropertyChanged(e.NewItems);
break;
}
base.OnCollectionChanged(e);
}
/// <summary>
/// Raises <see cref="OnCollectionChanged"/> when an item property change.
/// </summary>
/// <param name="sender">The object where the event handler is attached.</param>
/// <param name="e">The event data.</param>
private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
/// <summary>
/// Register <see cref="Item_PropertyChanged"/> on each item in the list.
/// </summary>
/// <param name="items">The list of items.</param>
private void RegisterPropertyChanged(IList items)
{
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged += Item_PropertyChanged;
}
}
}
/// <summary>
/// Unegister <see cref="Item_PropertyChanged"/> on each item in the list.
/// </summary>
/// <param name="items">The list of items.</param>
private void UnregisterPropertyChanged(IList items)
{
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged -= Item_PropertyChanged;
}
}
}
}
}
using System;
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApp11
{
internal class Program
{
//Implemented based on interface, not part of algorithm
public static string RemoveAllNamespaces(string xmlDocument)
{
XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument));
return xmlDocumentWithoutNs.ToString();
static XElement RemoveAllNamespaces(XElement e)
{
return new XElement(e.Name.LocalName,
(from n in e.Nodes()
select ((n is XElement) ? RemoveAllNamespaces(n as XElement) : n)),
(e.HasAttributes) ?
(from a in e.Attributes()
where (!a.IsNamespaceDeclaration && a.Name != "Name" && a.Value != "0")
select new XAttribute(a.Name.LocalName, a.Value)) : null);
}
}
private static void Main(string[] args)
{
Console.WriteLine(RemoveAllNamespaces("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<!--This file is compatible with Silverlight-->\r\n<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Name=\"svg3741\" Width=\"600\" Height=\"600\">\r\n <Canvas.RenderTransform>\r\n <TranslateTransform X=\"0\" Y=\"0\"/>\r\n </Canvas.RenderTransform>\r\n <Canvas.Resources/>\r\n <!--Unknown tag: metadata-->\r\n <!--Unknown tag: sodipodi:namedview-->\r\n <Ellipse xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Canvas.Left=\"0\" Canvas.Top=\"0\" Width=\"600\" Height=\"600\" Name=\"circle3731\" Fill=\"#FFAAE0FA\"/>\r\n <Canvas Name=\"g3739\">\r\n <Canvas.RenderTransform>\r\n <TranslateTransform X=\"-239.15\" Y=\"186.79\"/>\r\n </Canvas.RenderTransform>\r\n <Path xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Name=\"path3733\" Fill=\"#FFCBC2BF\" Data=\"m751.2-99c54.3 54.3 87.9 129.3 87.9 212.2 0 165.7-134.3 300-300 300-82.8 0-157.8-33.6-212.1-87.9\"/>\r\n <Path xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Name=\"path3735\" Fill=\"#FF061922\" Data=\"m485.8 102.2c-16.1 16.4-36 24.6-59.7 24.6-26.5 0-47.6-9.1-63.1-27.2-14.7-17.3-22.1-39.4-22.1-66.2 0-28.8 12.5-61.7 37.6-98.6 20.5-30.3 44.5-56.8 72.2-79.6-4 18.5-6 31.6-6 39.4 0 17.9 5.6 35.3 16.9 52.3 13.8 20.2 24.4 35.2 31.6 45 11.2 17 16.9 33.6 16.9 49.7 0 24-8.1 44.1-24.2 60.6m-0.4-92.3c-4.3-9.7-9.4-16.1-15.1-19.3 0.9 1.7 1.3 4.2 1.3 7.4 0 6.1-1.7 14.7-5.2 25.9l-5.6 17.3c0 10.1 5.1 15.1 15.1 15.1 10.7 0 16-7.1 16-21.2 0-7.2-2.2-15.6-6.5-25.3\"/>\r\n <Path xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Name=\"path3737\" Fill=\"#FF130C0E\" Data=\"m776.9 209.2c0 16.2-5.9 27.3-17.7 33.2-3.4 1.7-14.2 4.1-32.4 7-11.8 2-17.7 6.5-17.7 13.6l0 29.8c0 1.3 0.4 5.1 1.1 11.4l1.1 11.8c0 3.7-0.9 9.7-2.6 18.1-4.7 1-10.1 2.1-16.2 3.3-2-7.4-2.9-12.4-2.9-15.1 0-1.2 0.3-3.1 0.9-5.5 0.6-2.4 0.9-4.3 0.9-5.5 0-1.7-1.5-6.5-4.6-14.4l-5.7 0c-0.8 1.2-1 2.8-0.8 4.8 1 4.2 1.4 7.7 1.1 10.7-4.2 2.9-9.9 6.9-17.3 11.8-1.7-0.5-2.3-0.7-1.8-0.7l0-26.1c-0.5-1.2-1.7-1.7-3.7-1.5l-4.4 0-4.4 34.6c-3.4 0.2-7.6 0.2-12.5 0-1.7-8.1-4.8-20.1-9.2-36.1l-3 0c-2.7 8.6-4.2 13.3-4.4 14 0 1 0.3 2.9 0.9 5.7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1 17.1 2.9 19.5 2-0.5 4.2-1.4 6.6-2.6 1-0.5 3.8-0.7 8.5-0.7 4.7 0 10.3 1.5 16.9 4.4 2.5 0 3.7-6.6 3.7-19.9m83.3-51.3c0-9.9-3.7-18.7-11-26.5-7.4-7.8-15.8-11.7-25.4-11.7-10.3 0-20 4.7-28.9 14.2-9 9.5-13.5 19.3-13.5 29.7 0 8.6 4.2 12.9 12.5 12.9l42.4 0c16-0.2 24-6.4 24-18.5\"/>\r\n </Canvas>\r\n</Canvas>\r\n"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment