您的位置:首页 > Web前端 > Node.js

C#学习经典方法之(八)---XmlNode.OuterXml属性

2016-12-23 09:21 471 查看
XmlNode.OuterXml属性
获取包含此节点及其所有子节点的标记。
命名空间:
System.Xml
程序集: System.Xml(位于 System.Xml.dll)
语法:

public virtual string OuterXml { get; }

属性值

Type: System.String
包含此节点及其所有子节点的标记。

说明
OuterXml 不会返回默认属性。

备注
此属性是文档对象模型 (DOM) 的 Microsoft 扩展。

示例
下面的示例比较输出 InnerXmlOuterXml 属性。
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 不会返回默认属性。

异常
ExceptionCondition
InvalidOperationException在节点上设置此属性不能有子节点。
XmlException设置此属性时指定的 XML 格式不正确。

备注
尝试从一个节点的子节点不能设置此属性,例如是文本节点,引发的异常。 否则,设置 InnerXml 替换给定字符串的分析内容的子节点,该节点。 分析在当前命名空间上下文中完成。
此属性是文档对象模型 (DOM) 的 Microsoft 扩展。

说明
InnerXml 不是有效的方法来修改 DOM 替换复杂节点时,则可能存在性能问题。 更有效,构建节点并使用方法,如 InsertBefore, ,InsertAfter, ,AppendChild, ,和 RemoveChild 以修改 Xml 文档。

示例
下面的示例比较 InnerTextInnerXml 属性。
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 扩展。

示例
下面的示例比较 InnerTextInnerXml 属性。
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  XmlNode.InnerXml Out