XmlElement to string conversion

35,402

Solution 1

This will get the content of the element if the content is text:

element.Value

This will get the content of the element as XML:

element.InnerXml

This will get the element and its content as XML

element.OuterXml

Solution 2

You can look at the Value or InnerText properties of the element.

However, without further details of exactly what you are looking, I can't help more.

Update:

Seeing as you want the XML of all nodes, using InnerXml or OuterXml should do nicely.

Solution 3

Let's say you have this XmlElement:

<node>
  Hello
  <effect color="pink">
    World
  </effect>
</node>

With Console.Write(xmlElement.Inner) you see the inside of your node:

Hello <effect color="pink">World</effect>

With Console.Write(xmlElement.Outer) you get everything:

<node>Hello <effect color="pink">World</effect></node>

With Console.Write(xmlElement.Value) you get nothing, because Value always returns null for an XML element.

Share:
35,402

Related videos on Youtube

Night Walker
Author by

Night Walker

Updated on March 07, 2020

Comments

  • Night Walker
    Night Walker about 4 years

    Is there some simple way to convert XmlElement to string ?

    • Jon Skeet
      Jon Skeet almost 13 years
      What do you want the result to be?
    • Night Walker
      Night Walker almost 13 years
      Jon Skeet Just some string with all the nodes , I need it for logging
    • Jon Skeet
      Jon Skeet almost 13 years
      All the nodes in what format? Please give an example. For example, does the OuterXml property give you what you need?
  • Sivaraman
    Sivaraman about 6 years
    Explaining it with the Example gives more insight. Thanks. Voted Up!