简体   繁体   中英

Get entire node XML rather than InnerXml

Given the following XML:

<Root>
  <Item id="1">
    <name>Foo</name>
    <status>Active</status>
  </Item>
  <Item id="2">
    <name>Bar</name>
    <status>Inactive</status>
  </Item>
</Root>

Let's say I have this XML in an XmlDocument object and then have the following code:

var nodes = xmlDocumentObject.GetElementsByTagName("Item");
foreach (var node in nodes)
{
   var nodeXml = ??
}

I can easily get the InnerXml of each node, which for the first node would be:

<name>Foo</name>
<status>Active</status>

But how can I get the XML for the node including the containing tag and its attributes, such as this:

<Item id="1">
  <name>Foo</name>
  <status>Active</status>
</Item>

Try using XmlNode.OuterXml instead of InnerXml :

foreach (XmlNode node in nodes)
{
   var nodeXml = node.OuterXml;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM