You get the same results because you are at the #document node, which is not
the first element. Here is an example that shows the difference. At the
document level, InnerXml and OuterXml *appear* to have the same output
(OuterXml is actually the entire document, while InnerXml is a document
fragment). At the node level, this is much more apparent:
using System;
using System.Xml;
namespace CSConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root><child id=\"1\">foo</child><child
id=\"2\">foo</child></root>");
OutputNode(doc);
XmlNode node = doc.SelectSingleNode("root/child[@id=\"1\"]");
OutputNode(node);
}
static void OutputNode(XmlNode node)
{
Console.WriteLine ("InnerXML of node = " + node.Name + ": " +
node.InnerXml);
Console.WriteLine ("OuterXML of node = " + node.Name + ": " +
node.OuterXml); Console.WriteLine("{0}",new
String(Char.Parse("="),15));
}
}
}
The output of this example is:
InnerXML of node = #document: <root><child id="1">foo</child><child
id="2">foo</child></root>
OuterXML of node = #document: <root><child id="1">foo</child><child
id="2">foo</child></root>
===============
InnerXML of node = child: foo
OuterXML of node = child: <child id="1">foo</child>
===============
Notice that when a single node was passed into the OutputNode method, the
InnerXml returned was all of the node's inner XML, which in this case was a
single text node. OuterXml returned the current node plus all of its child
nodes.
Kirk Allen Evans
http://www.xmlandasp.net
"XML and ASP.NET", New Riders Publishing
http://www.amazon.com/exec/obidos/ASIN/073571200X
> -----Original Message-----
> From: dotnet discussion [mailto:[EMAIL PROTECTED]]On Behalf Of
> Nischal Muthana
> Sent: Friday, May 31, 2002 1:19 PM
> To: [EMAIL PROTECTED]
> Subject: [DOTNET] XmlDocument.OuterXML
>
>
> Hi All
>
> I am trying to get back the XmlDocument as a string. I am using the
>
> xDoc.OuterXML;
> where xDoc is my XmlDocument.
>
> But the OuterXML seems to work like InnerXML, returning me only
> the data in
> the nodes. I want to whole of the XML including the data and the
> nodes too.
> Can someone let me know where I am goin wrong.
>
> Thanks
> Nischal
>
> You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>
>
You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.