Convert XElement to XmlNode (and Convert XmlNode to XElement){转载}
来源:互联网 发布:day one 2 mac 破解版 编辑:程序博客网 时间:2024/05/17 06:25
本文转载自:http://blogs.msdn.com/ericwhite/archive/2008/12/22/convert-xelement-to-xmlnode-and-convert-xmlnode-to-xelement.aspx
Sometimes you want to convert an XmlNode to an XElement and back again. Some programming libraries define methods that take XmlNode objects as parameters. These libraries also may contain properties and methods that return XmlNode objects. However, it is more convenient to work with LINQ to XML instead of the classes in System.Xml (XmlDocument, XmlNode, etc.) This post presents a bit of code to do these conversions.(Update March 5, 2009 - I've written a blog post that shows how to convert from XDocument to XmlDocument (and vice versa). The code presented in that post is a superset of the code presented in this code.)As an example of where you need these methods, you can do a lot with SharePoint using web services. The proxy classes that wsdl.exe creates contain methods that use classes in System.Xml.It is convenient to write these conversions as extension methods. When converting to and from classes in System.Xml, the code reads much better when you tack an extension method on the end instead of surrounding the expression with a method call. The following code shows how to call the GetListItems method of the Lists web service using the GetXmlNode and GetXElement extension methods:XElement queryOptions = new XElement("QueryOptions", new XElement("Folder", "Open XML Documents"), new XElement("ViewAttributes", new XAttribute("Scope", "Recursive")));XElement viewFields = new XElement("ViewFields", new XElement("FieldRef", new XAttribute("Name", "GUID")), new XElement("FieldRef", new XAttribute("Name", "ContentType")), new XElement("FieldRef", new XAttribute("Name", "BaseName")), new XElement("FieldRef", new XAttribute("Name", "Modified")), new XElement("FieldRef", new XAttribute("Name", "EncodedAbsUrl")));XElement listContent = wsList .GetListItems(documentLibraryGUID, null, null, viewFields.GetXmlNode(), null, queryOptions.GetXmlNode(), webId) .GetXElement();To create an XmlNode from an XElement, you create an XmlReader using the XNode.CreateReader method, create an XmlDocument, and load the document using the XmlReader. XmlDocument inherits XmlNode.To create an XElement from an XmlNode, you create an XDocument, create an XmlWriter using the XContainer.CreateWriter method, write the XmlNode to the XDocument, and return the root element of the XDocument.The following example contains the extension methods and some code that shows their use:using System;using System.Collections.Generic;using System.Linq;using System.Xml;using System.Xml.Linq;public static class MyExtensions{ public static XElement GetXElement(this XmlNode node) { XDocument xDoc = new XDocument(); using (XmlWriter xmlWriter = xDoc.CreateWriter()) node.WriteTo(xmlWriter); return xDoc.Root; } public static XmlNode GetXmlNode(this XElement element) { using (XmlReader xmlReader = element.CreateReader()) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlReader); return xmlDoc; } }}class Program{ static void Main(string[] args) { XElement e = new XElement("Root", new XElement("Child", new XAttribute("Att", "1") ) ); XmlNode xmlNode = e.GetXmlNode(); Console.WriteLine(xmlNode.OuterXml); XElement newElement = xmlNode.GetXElement(); Console.WriteLine(newElement); }}Code is attached..csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
- Convert XElement to XmlNode (and Convert XmlNode to XElement){转载}
- XmlNode Deserialize to object
- Linq To Xml 备忘录1(XDocument, XElement的使用)
- Linq to Xml:将XML字符串转化成 XElement 类型
- Linq To XML:论XStreamingElement与XElement的不同
- Linq To XML:论XStreamingElement与XElement的不同
- Linq to XML 之XElement的Descendants方法的新发现
- Custom Entity And XElement Convertion
- Matrix to convert between HSL and RGB
- Matrix to convert between HSV and RGB
- How to convert between TDes8 and TDes16
- char* convert to string and back
- java convert timestamp to date and time
- XElement类
- C# Linq to xml学习 用XElement类读写XML文档(用作配置文档)
- How to load FIBITMAP and convert it to HBITMAP
- Convert Image to Base64 String and Base64 String to Image
- Convert object to json and json to object in C#
- 再谈CLR:关于类型的拷问
- 再谈CLR:事件定义
- 对称加密的例子(DES)——直接加密和解密字符串
- 对称加密的例子(DES)——针对文件进行加解密
- new修饰符之惑
- Convert XElement to XmlNode (and Convert XmlNode to XElement){转载}
- 安装电脑看PPS
- 使用BT3破解WEP密码详细教程
- 如何配置SQL2008,让其允许C#远程外部连接。
- 2010年10月20日
- 发送arp数据包
- 成功人生40条
- 笛卡尔积、除、(外)连接等重要关系代数求解方法 概述
- 敛财大亨洛克菲勒的十六点启示