问题描述
OuterXml - 获取表示当前节点及其所有子节点的 XML 标记.
InnerXml - 获取仅表示当前节点的子节点的 XML 标记.
InnerXml - gets the XML markup representing only the child nodes of the current node.
但是对于 XMLDocument
真的重要吗?(结果明智,我知道这无关紧要,但在逻辑上?).
But for XMLDocument
does it really matter? (result-wise, well I know it doesn't matter, but logically?).
示例:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
string xmlresponse = doc.OuterXml;
string xmlresponse2 = doc.InnerXml;
简单来说,虽然 xmlresponse
和 xmlresponse2
在上面的代码中是相同的.我应该更喜欢使用 OuterXml
还是 InnerXml
?
In simple words, though both xmlresponse
and xmlresponse2
will be the same in the code above. Should I prefer using OuterXml
or InnerXml
?
推荐答案
如果你想找出为什么 XmlDocument 的 OuterXml 和 InnerXml 是相同的:看看 XmlDocument 代表什么节点 - 它是整个 Xml 树的父节点.但它本身没有任何视觉表现——所以我"+儿童内容"因为它与儿童内容"相同.
If you are trying to find why they OuterXml and InnerXml are the same for XmlDocument: look at what XmlDocument represents as node - it is parent of whole Xml tree. But by itself it does not have any visual representation - so "Me"+ "content of children" for it is the same as "content of children".
您可以编写基本代码来遍历 XmlNode + 子节点并传递 XmlDocument 以查看其行为方式:
You can write basic code to walk XmlNode + children and pass XmlDocument to see why it behaves this way:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<?xml version='1.0' ?><root><item>test</item></root>");
Action<XmlNode, string> dump=null;
dump = (root, prefix) => {
Console.WriteLine("{0}{1} = {2}", prefix, root.Name, root.Value);
foreach (XmlNode n in root.ChildNodes)
{
dump(n, " " + prefix);
}
};
dump(doc,"");
输出显示 XmlDocument XmlDocument 本身没有任何内容具有可视化表示,并且第一个具有文本表示的节点是它的子节点:
Output shows that XmlDocument there is nothing in XmlDocument itself that have visual representation and the very first node that have text representation is child of it:
#document =
xml = version="1.0"
root =
item =
#text = test
这篇关于XMLDocument,innerxml 和outerxml 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!