//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license
#region using
using System;
using System.Xml;
#endregion
namespace DbmlManager.Lib.Utility
{
#region Class Docs
/// <summary>
/// Summary description for XmlUtil.
/// </summary>
#endregion
public class XmlUtil
{
#region LoadString(ref string val, XmlNode parent, string nodeName)
public static void LoadString(ref string val, XmlNode parent, string nodeName)
{
try
{
XmlElement elem = parent[nodeName];
if (elem != null)
val = elem.InnerText.Trim();
}
catch
{
val = string.Empty;
}
}
#endregion
}
}
XML-RPC
Create Xml Document, Node
//GNU General Public License version 2 (GPLv2)
//http://cbasetest.codeplex.com/license
namespace SDFL.Helper
{
using System;
using System.Text;
using System.Xml;
public class XMLHelper
{
public static XmlDocument CreateXmlDocument()
{
return CreateXmlDocument(new Version(1, 0), Encoding.UTF8);
}
public static XmlDocument CreateXmlDocument(Version version, Encoding encoding)
{
XmlDocument document = new XmlDocument();
document.AppendChild(document.CreateXmlDeclaration(version.ToString(), encoding.BodyName, string.Empty));
return document;
}
public static XmlNode CreateXmlNode(XmlDocument xmlDocument, string name)
{
return CreateXmlNode(xmlDocument, name, string.Empty);
}
public static XmlNode CreateXmlNode(XmlDocument xmlDocument, string name, string value)
{
XmlNode node = xmlDocument.CreateNode(XmlNodeType.Element, name, string.Empty);
node.AppendChild(xmlDocument.CreateTextNode(value));
return node;
}
}
}
DOM feature check
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));
}
}
}
}
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;
}
}
}