csharp XML扩展类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp XML扩展类相关的知识,希望对你有一定的参考价值。


using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace xmlSandbox.cs.Business
{
    /// <summary>
    /// Adds the <see cref="ToXmlDocument"/> to all <see cref="XDocument">XDocuments</see> 
    /// and <see cref="ToXDocument"/>  methods to all <see cref="XmlDocument">XmlDocuments</see> in this project.
    /// </summary>
    public static class XmlExtensions
    {
        /// <summary>
        /// Extended Method used to convert a <see cref="XDocument"/> to a <see cref="XmlDocument"/>
        /// </summary>
        public static XmlDocument ToXmlDocument(this XDocument xDocument)
        {
            var xmlDocument = new XmlDocument();
            using (var xmlReader = xDocument.CreateReader())
            {
                xmlDocument.Load(xmlReader);
            }
            return xmlDocument;
        }

        /// <summary>
        /// Extended Method used to convert a <see cref="XmlDocument"/> to a <see cref="XDocument"/>
        /// </summary>
        public static XDocument ToXDocument(this XmlDocument xmlDocument)
        {
            using (var nodeReader = new XmlNodeReader(xmlDocument))
            {
                nodeReader.MoveToContent();
                return XDocument.Load(nodeReader);
            }
        }

        /// <summary>
        /// Extended Method used to convert a <see cref="XmlNode"/> to a <see cref="XElement"/>
        /// </summary>
        public static XElement ToXElement(this XmlNode node)
        {
            var xDoc = new XDocument();
            using (var xmlWriter = xDoc.CreateWriter())
                node.WriteTo(xmlWriter);
            return xDoc.Root;
        }

        /// <summary>
        /// Extended Method used to convert a <see cref="XElement"/> to a <see cref="XmlNode"/>
        /// </summary>
        public static XmlNode ToXmlNode(this XElement element)
        {
            using (var xmlReader = element.CreateReader())
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlReader);
                return xmlDoc;
            }
        }

        /// <summary>
        /// Extended Method used to Check if Element is Null
        /// </summary>
        /// <param name="xElement">Element to Check as <see cref="XElement"/></param>
        /// <returns>Value or Null</returns>
        public static dynamic ToValue(this XElement xElement)
        {
            return xElement != null ? xElement.Value : null;
        }

        /// <summary>
        ///  Deserialize an xml into a serializable class
        /// </summary>
        private static T ConvertNode<T>(this XmlNode node) where T : class
        {
            var stm = new MemoryStream();

            var stw = new StreamWriter(stm);
            stw.Write(node.OuterXml);
            stw.Flush();

            stm.Position = 0;

            var ser = new XmlSerializer(typeof(T));
            var result = (ser.Deserialize(stm) as T);

            return result;
        }
    }
}

以上是关于csharp XML扩展类的主要内容,如果未能解决你的问题,请参考以下文章

csharp 使用适当的缩进来扩展您的xml文档的扩展

csharp Xml Validator类

csharp WPF控件动画扩展类

如何在 XML 布局中使用扩展类?

二进制 XML 文件第 13 行:在扩展 VideoView 的类上膨胀类时出错

csharp C#:在.NET中合并,追加,扩展两个数组(csharp,mono)