Converting XElement into XmlNode

前端 未结 5 1937
死守一世寂寞
死守一世寂寞 2021-02-12 07:17

I know there is no direct method of doing it but still.. Can we convert XElement element into XmlNode. Options like InnerText and

5条回答
  •  失恋的感觉
    2021-02-12 07:48

    I use the following extension methods, they seem to be quite common:

    public static class MyExtensions
    {
        public static XElement ToXElement(this XmlNode node)
        {
            XDocument xDoc = new XDocument();
            using (XmlWriter xmlWriter = xDoc.CreateWriter())
                node.WriteTo(xmlWriter);
            return xDoc.Root;
        }
    
        public static XmlNode ToXmlNode(this XElement element)
        {
            using (XmlReader xmlReader = element.CreateReader())
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlReader);
                return xmlDoc;
            }
        }
    }
    

提交回复
热议问题