using System;
using System.Xml;
class DomFeatureChecker {
private static readonly string [] versions = new string [] {
"1.0", "2.0" };
private static readonly string [] features = new string [] {
"Core", "XML", "HTML", "Views", "Stylesheets", "CSS",
"CSS2", "Events", "UIEvents", "MouseEvents", "MutationEvents",
"HTMLEvents", "Range", "Traversal" };
public static void Main(string[] args) {
XmlImplementation impl = new XmlImplementation();
foreach (string version in versions) {
foreach (string feature in features) {
Console.WriteLine("{0} {1}={2}", feature, version,
impl.HasFeature(feature, null));
}
}
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Find a single Attribute of the type 'attributeType' from the supplied class
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
namespace Spring.Util
{
/// <summary>
/// General utility methods for working with annotations
/// </summary>
public class AttributeUtils
{
/// <summary>
/// Find a single Attribute of the type 'attributeType' from the supplied class,
/// traversing it interfaces and super classes if no attribute can be found on the
/// class iteslf.
/// </summary>
/// <remarks>
/// This method explicitly handles class-level attributes which are not declared as
/// inherited as well as attributes on interfaces.
/// </remarks>
/// <param name="type">The class to look for attributes on .</param>
/// <param name="attributeType">Type of the attribibute to look for.</param>
/// <returns>the attribute of the given type found, or <code>null</code></returns>
public static Attribute FindAttribute(Type type, Type attributeType)
{
Attribute[] attributes = Attribute.GetCustomAttributes(type, attributeType, false); // we will traverse hierarchy ourselves.
if (attributes.Length > 0)
{
return attributes[0];
}
foreach (Type interfaceType in type.GetInterfaces())
{
Attribute attrib = FindAttribute(interfaceType, attributeType);
if (attrib != null)
{
return attrib;
}
}
if (type.BaseType == null)
{
return null;
}
return FindAttribute(type.BaseType, attributeType);
}
}
}
Get Attribute With Conversion
//http://validationframework.codeplex.com/
//License: Microsoft Permissive License (Ms-PL) v1.1
using System;
using System.Xml;
namespace ValidationFramework.Extensions
{
public static class XmlElementExtensions
{
/// <summary>
/// weakly typed
/// </summary>
/// <param name="element"></param>
/// <param name="key"></param>
/// <returns></returns>
internal static object GetAttributeWithConversion(this XmlElement element, Type typeToConvertTo, string key)
{
if (!element.HasAttribute(key))
throw new ArgumentOutOfRangeException(string.Format("The key '{0}' cannot be found in xml element.", key));
var stringValue = element.GetAttribute(key);
var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeToConvertTo);
return converter.ConvertFromString(stringValue);
}
}
}
Remove all the xml namespaces (xmlns) attributes in the xml string
#region License and Copyright
/*
* Dotnet Commons Xml
*
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place,
* Suite 330,
* Boston,
* MA 02111-1307
* USA
*
*/
#endregion
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Serialization;
//using Dotnet.Commons.Reflection;
namespace Dotnet.Commons.Xml
{
///
/// <summary>
/// This utility class contains wrapper functions that help to ease the handling and
/// manipulation of Xml documents, such as adding an element, adding an attribute
/// to an element, copying and cloning of nodes, etc.
///
/// </summary>
///
public abstract class XmlUtils
{
/// <summary>
/// Remove all the xml namespaces (xmlns) attributes in the xml string
/// </summary>
/// <param name="xmlData"></param>
/// <returns></returns>
public static string RemoveAllXmlNamespace(string xmlData)
{
string xmlnsPattern = "s+xmlnss*(:w)?s*=s*"(?<url>[^"]*)"";
MatchCollection matchCol = Regex.Matches(xmlData, xmlnsPattern);
foreach (Match m in matchCol)
{
xmlData = xmlData.Replace(m.ToString(), "");
}
return xmlData;
}
}
}
Gets a string from an attribute or node.
//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace Redwerb.BizArk.Core.XmlExt
{
/// <summary>
/// Provides extension methods for processing Xml.
/// </summary>
public static class XmlExt
{
/// <summary>
/// Gets a string from an attribute or node.
/// </summary>
/// <param name="node"></param>
/// <param name="name"></param>
/// <param name="dfltVal"></param>
/// <returns></returns>
public static string GetString(this XmlNode node, string name, string dfltVal)
{
var att = node.Attributes[name];
if (att != null) return att.Value;
var child = node.SelectSingleNode(name);
if (child != null) return child.InnerText;
return dfltVal;
}
}
}
Attempts to get and convert an attribute from an XmlElement
//http://validationframework.codeplex.com/
//License: Microsoft Permissive License (Ms-PL) v1.1
using System;
using System.Xml;
namespace ValidationFramework.Extensions
{
public static class XmlElementExtensions
{
/// <summary>
/// Attempts to get and convert an attribute from a <see cref="XmlElement"/>. If the item does not exist or can't be converted a <paramref name="defaultValue"/> is returned.
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> to try to convert to.</typeparam>
/// <param name="element">The <see cref="XmlElement"/> to extract the attribute value from.</param>
/// <param name="key">The key to use or the extraction</param>
/// <param name="defaultValue">The default value if <paramref name="key"/> is not found.</param>
/// <returns>The value from <paramref name="attributes"/> if <paramref name="key"/> exists; otherwise <paramref name="defaultValue"/>.</returns>
internal static T GetAttribute<T>(this XmlElement element, string key, T defaultValue)
{
if (!element.HasAttribute(key))
return defaultValue;
var stringValue = element.GetAttribute(key);
var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
if (!converter.CanConvertFrom(typeof(string)))
return defaultValue;
return (T)converter.ConvertFromString(stringValue);
}
}
}
Attempts to get and cast attribute from an XmlElement
//http://validationframework.codeplex.com/
//License: Microsoft Permissive License (Ms-PL) v1.1
using System;
using System.Xml;
namespace ValidationFramework.Extensions
{
public static class XmlElementExtensions
{
/// <summary>
/// Attempts to get and cast attribute from a <see cref="XmlElement"/>. If the item does not exist or can't be casted exceptions are thrown.
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> to try to convert to.</typeparam>
/// <param name="element">The <see cref="XmlElement"/> to extract the attribute value from.</param>
/// <param name="key">The key to use or the extraction</param>
/// <param name="defaultValue">The default value if <paramref name="key"/> is not found.</param>
/// <returns>The value from <paramref name="attributes"/> if <paramref name="key"/> exists; otherwise <paramref name="defaultValue"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
internal static T GetAttribute<T>(this XmlElement element, string key)
{
if (!element.HasAttribute(key))
throw new ArgumentOutOfRangeException(string.Format("The key '{0}' cannot be found in xml element.", key));
var stringValue = element.GetAttribute(key);
var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
return (T)converter.ConvertFromString(stringValue);
}
}
}