XmlNode.InnerXml Egenskap

Definition

Hämtar eller anger markering som endast representerar de underordnade noderna i den här noden.

public:
 virtual property System::String ^ InnerXml { System::String ^ get(); void set(System::String ^ value); };
public virtual string InnerXml { get; set; }
member this.InnerXml : string with get, set
Public Overridable Property InnerXml As String

Egenskapsvärde

Markering av de underordnade noderna i den här noden, inklusive inte standardattribut.

Undantag

Ange den här egenskapen på en nod som inte kan ha underordnade noder.

DEN XML som anges när du anger den här egenskapen är inte välformulerad.

Exempel

I följande exempel jämförs InnerText egenskaperna och InnerXml .

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 );
  }
}
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

Kommentarer

Om du försöker ange den här egenskapen från en nod som inte kan ha underordnade noder, till exempel en textnod, genereras ett undantag. Annars ersätter inställningen InnerXml de underordnade noderna i noden med det tolkade innehållet i den angivna strängen. Parsningen görs i den aktuella namnområdeskontexten.

Den här egenskapen är ett Microsoft tillägg till dokumentobjektmodellen (DOM).

Note

InnerXml är inte ett effektivt sätt att ändra DOM. Det kan uppstå prestandaproblem när du ersätter komplexa noder. Det är mer effektivt att konstruera noder och använda metoder som InsertBefore, InsertAfter, AppendChildoch RemoveChild att ändra XML-dokumentet.

Gäller för