C# XMLDocument,innerxml 和outerxml 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12578609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:45:45  来源:igfitidea点击:

XMLDocument, difference between innerxml and outerxml

c#xml

提问by InfantPro'Aravind'

OuterXml- gets the XML markup representing the current node and all its child nodes.

InnerXml- gets the XML markup representing only the child nodes of the current node.

OuterXml- 获取表示当前节点及其所有子节点的 XML 标记。

InnerXml- 获取仅表示当前节点的子节点的 XML 标记。

But for XMLDocumentdoes it really matter? (result-wise, well I know it doesn't matter, but logically?).

但这XMLDocument真的重要吗?(结果明智,我知道这无关紧要,但在逻辑上?)。

Example:

例子:

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;

In simple words, though both xmlresponseand xmlresponse2will be the same in the code above. Should I prefer using OuterXmlor InnerXml?

简单来说,虽然在上面的代码中两者xmlresponsexmlresponse2将是相同的。我应该更喜欢使用OuterXml还是InnerXml

采纳答案by Alexei Levenkov

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".

如果您想找出为什么 XmlDocument 的 OuterXml 和 InnerXml 相同:请查看 XmlDocument 表示为节点的内容 - 它是整个 Xml 树的父节点。但它本身没有任何视觉表现——所以“我”+“儿童内容”与“儿童内容”相同。

You can write basic code to walk XmlNode + children and pass XmlDocument to see why it behaves this way:

您可以编写基本代码来遍历 XmlNode + 子项并传递 XmlDocument 以查看其行为方式:

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,"");

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:

输出显示 XmlDocument XmlDocument 本身没有任何具有视觉表示的内容,并且具有文本表示的第一个节点是它的子节点:

#document = 
  xml = version="1.0"
  root = 
    item = 
      #text = test

回答by vCillusion

For case where InnerXml equals the OuterXml the following solution will work out if you wanted the InnerXml:

对于 InnerXml 等于 OuterXml 的情况,如果您想要 InnerXml,以下解决方案将起作用:

// Create a new Xml doc object with root node as "NewRootNode" and 
// copy the inner content from old doc object using the LastChild.
                    XmlDocument doc = new XmlDocument("FileName");
                    XmlElement newRoot = docNew.CreateElement("NewRootNode");
                    docNew.AppendChild(newRoot);
// The below line solves the InnerXml equals the OuterXml Problem
                    newRoot.InnerXml = oldDoc.LastChild.InnerXml;
                    string xmlText = docNew.OuterXml;