C#学习经典方法之(八)---XmlNode.OuterXml属性
2016-12-23 09:21
471 查看
XmlNode.OuterXml属性
获取包含此节点及其所有子节点的标记。
命名空间:
System.Xml
程序集: System.Xml(位于 System.Xml.dll)
语法:
包含此节点及其所有子节点的标记。
备注
此属性是文档对象模型 (DOM) 的 Microsoft 扩展。
示例
下面的示例比较输出 InnerXml 和 OuterXml 属性。
备注:转自https://msdn.microsoft.com/zh-cn/library/system.xml.xmlnode.outerxml(v=vs.110).aspx
****************************************************
XmlNode.InnerXml属性
获取或设置仅表示该节点的子节点的标记。
命名空间:
System.Xml
程序集: System.Xml(位于 System.Xml.dll)
语法
C#
public virtual string InnerXml { get; set; }
该节点的子节点的标记。
异常
备注
尝试从一个节点的子节点不能设置此属性,例如是文本节点,引发的异常。 否则,设置 InnerXml 替换给定字符串的分析内容的子节点,该节点。 分析在当前命名空间上下文中完成。
此属性是文档对象模型 (DOM) 的 Microsoft 扩展。
示例
下面的示例比较 InnerText 和 InnerXml 属性。
********************************************************
XmlNode.InnerText属性
获取或设置节点及其所有子节点的串连值。
命名空间:
System.Xml
程序集: System.Xml(位于 System.Xml.dll)
语法:
节点及其所有子节点的串连值。
备注
设置此属性将用给定字符串的分析内容替换所有子节点。
对于叶节点, InnerText 返回相同的内容 Value 属性。
此属性是文档对象模型 (DOM) 的 Microsoft 扩展。
示例
下面的示例比较 InnerText 和 InnerXml 属性。
获取包含此节点及其所有子节点的标记。
命名空间:
System.Xml
程序集: System.Xml(位于 System.Xml.dll)
语法:
public virtual string OuterXml { get; }
属性值
Type: System.String包含此节点及其所有子节点的标记。
说明 |
---|
OuterXml 不会返回默认属性。 |
此属性是文档对象模型 (DOM) 的 Microsoft 扩展。
示例
下面的示例比较输出 InnerXml 和 OuterXml 属性。
using System; using System.IO; using System.Xml; public class Sample { public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>"); XmlNode root = doc.DocumentElement; // OuterXml includes the markup of current node. Console.WriteLine("Display the OuterXml property..."); Console.WriteLine(root.OuterXml); // InnerXml does not include the markup of the current node. // As a result, the attributes are not displayed. Console.WriteLine(); Console.WriteLine("Display the InnerXml property..."); Console.WriteLine(root.InnerXml); } }
备注:转自https://msdn.microsoft.com/zh-cn/library/system.xml.xmlnode.outerxml(v=vs.110).aspx
****************************************************
XmlNode.InnerXml属性
获取或设置仅表示该节点的子节点的标记。
命名空间:
System.Xml
程序集: System.Xml(位于 System.Xml.dll)
语法
C#
public virtual string InnerXml { get; set; }
属性值
Type: System.String该节点的子节点的标记。
说明 |
---|
InnerXml 不会返回默认属性。 |
Exception | Condition |
---|---|
InvalidOperationException | 在节点上设置此属性不能有子节点。 |
XmlException | 设置此属性时指定的 XML 格式不正确。 |
尝试从一个节点的子节点不能设置此属性,例如是文本节点,引发的异常。 否则,设置 InnerXml 替换给定字符串的分析内容的子节点,该节点。 分析在当前命名空间上下文中完成。
此属性是文档对象模型 (DOM) 的 Microsoft 扩展。
说明 |
---|
InnerXml 不是有效的方法来修改 DOM 替换复杂节点时,则可能存在性能问题。 更有效,构建节点并使用方法,如 InsertBefore, ,InsertAfter, ,AppendChild, ,和 RemoveChild 以修改 Xml 文档。 |
下面的示例比较 InnerText 和 InnerXml 属性。
using System; using System.Xml; public class Test { public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<root>"+ ="<elem>some text<child/>more text</elem>" + "</root>"); XmlNode elem = doc.DocumentElement.FirstChild; // Note that InnerText does not include the markup. Console.WriteLine("Display the InnerText of the element..."); Console.WriteLine( elem.InnerText ); // InnerXml includes the markup of the element. Console.WriteLine("Display the InnerXml of the element..."); Console.WriteLine(elem.InnerXml); // Set InnerText to a string that includes markup. // The markup is escaped. elem.InnerText = "Text containing <markup/> will have char(<) and char(>) escaped."; Console.WriteLine( elem.OuterXml ); // Set InnerXml to a string that includes markup. // The markup is not escaped. elem.InnerXml = "Text containing <markup/>."; Console.WriteLine( elem.OuterXml ); } }备注:转自https://msdn.microsoft.com/zh-cn/library/system.xml.xmlnode.innerxml(v=vs.110).aspx
********************************************************
XmlNode.InnerText属性
获取或设置节点及其所有子节点的串连值。
命名空间:
System.Xml
程序集: System.Xml(位于 System.Xml.dll)
语法:
public virtual string InnerText { get; set; }
属性值
Type: System.String节点及其所有子节点的串连值。
备注
设置此属性将用给定字符串的分析内容替换所有子节点。
对于叶节点, InnerText 返回相同的内容 Value 属性。
此属性是文档对象模型 (DOM) 的 Microsoft 扩展。
示例
下面的示例比较 InnerText 和 InnerXml 属性。
using System; using System.Xml; public class Test { public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<root>"+ "<elem>some text<child/>more text</elem>" + "</root>"); XmlNode elem = doc.DocumentElement.FirstChild; // Note that InnerText does not include the markup. Console.WriteLine("Display the InnerText of the element..."); Console.WriteLine( elem.InnerText ); // InnerXml includes the markup of the element. Console.WriteLine("Display the InnerXml of the element..."); Console.WriteLine(elem.InnerXml); // Set InnerText to a string that includes markup. // The markup is escaped. elem.InnerText = "Text containing <markup/> will have char(<) and char(>) escaped."; Console.WriteLine( elem.OuterXml ); // Set InnerXml to a string that includes markup. // The markup is not escaped. elem.InnerXml = "Text containing <markup/>."; Console.WriteLine( elem.OuterXml ); } }输出:
Display the InnerText of the element... some textmore text Display the InnerXml of the element... some text<child />more text <elem>Text containing <markup/> will have char(<) and char(>) escape d.</elem> <elem>Text containing <markup />.</elem>备注:转自https://msdn.microsoft.com/zh-cn/library/system.xml.xmlnode.innertext(v=vs.110).aspx
相关文章推荐
- ajax学习笔记---XMLHttpRequest的属性和方法
- c#学习笔记——抽象类、抽象方法、抽象属性
- [读书笔记]C#学习笔记五: C#3.0自动属性,匿名属性及扩展方法
- C#学习笔记(12)——三种方法操作XML
- c#学习摘录:类\继承\接口\字段、方法、属性\浅深复制(3)
- .Net (C#) 在学习中遇到的RichTextBox控件的一些属性和方法
- C#学习经典方法之(一)--装箱和取消装箱
- C#反射的学习(二)--访问字段、属性、方法
- C#学习经典方法之(五)--LINQ中的DataContext类
- C#学习笔记:控件BackColor属性与ForeColor的使用方法
- java学习脚印:xml中空白文本结点(whitespace TextNode)处理及验证方法
- C#操作XML方法:新增、修改和删除节点与属性
- java学习脚印:xml中空白文本结点(whitespace TextNode)处理及验证方法
- c#学习摘录:类\继承\接口\字段、方法、属性\浅深复制(1)
- 安卓学习之-ListView相关的XML属性及方法
- 【C#】C#操作XML方法:新增、修改和删除节点与属性
- C#学习笔记四: C#3.0自动属性&匿名属性及扩展方法
- C#可扩展编程之MEF学习笔记(三):导出类的方法和属性
- c#学习摘录:类\继承\接口\字段、方法、属性\浅深复制(4)
- c#学习摘录:类\继承\接口\字段、方法、属性\浅深复制(2)