Вопрос

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top