Converting XElement into XmlNode

前端 未结 6 1490
逝去的感伤
逝去的感伤 2021-02-12 06:56

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

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-12 07:28

    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;
            }
        }
    }
    

提交回复
热议问题