Export (0) Print
Expand All

XmlNode.InnerXml Property

Gets or sets the markup representing only the child nodes of this node.

[Visual Basic]
Public Overridable Property InnerXml As String
[C#]
public virtual string InnerXml {get; set;}
[C++]
public: __property virtual String* get_InnerXml();
public: __property virtual void set_InnerXml(String*);
[JScript]
public function get InnerXml() : String;
public function set InnerXml(String);

Property Value

The markup of the child nodes of this node.

Exceptions

Exception Type Condition
InvalidOperationException Setting this property on a node that cannot have child nodes.
XmlException The XML specified when setting this property is not well-formed.

Remarks

Attempting to set this property from a node that cannot have child nodes, for example a Text node, throws an exception. Otherwise, setting InnerXml replaces the child nodes, of the node, with the parsed contents of the given string. The parsing is done in the current namespace context.

This property is a Microsoft extension to the Document Object Model (DOM).

Example

[Visual Basic, C#, C++] The following example compares the InnerText and InnerXml properties.

[Visual Basic] 
Imports System
Imports System.Xml

public class Test

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<root>"& _
                "<elem>some text<child/>more text</elem>" & _
                "</root>")

    Dim elem as XmlNode = 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 )
    
  end sub
end class

[C#] 
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 );
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;

int main() 
{
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<root>"
                 S"<elem>some text<child/>more text</elem>" 
                 S"</root>");

    XmlNode* elem = doc->DocumentElement->FirstChild;

    // Note that InnerText does not include the markup.
    Console::WriteLine(S"Display the InnerText of the element...");
    Console::WriteLine(elem->InnerText);

    // InnerXml includes the markup of the element.
    Console::WriteLine(S"Display the InnerXml of the element...");
    Console::WriteLine(elem->InnerXml);

    // Set InnerText to a string that includes markup.  
    // The markup is escaped.
    elem->InnerText = S"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 = S"Text containing <markup/>.";
    Console::WriteLine(elem->OuterXml);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlNode Class | XmlNode Members | System.Xml Namespace

Show:
© 2014 Microsoft