Search code examples
c#xmlxelement

How to retrieve HTML from XElement without parent tag?


I have xml like this

XElement xe = "<root>
    <mynode>
        Text with link <a href=''>Test</a>
    </mynode>
</root>";

public static string GetHtmlFromXElement(this XElement xe)
{
    return xe.ToString();
}

If I use

string result = xe.Element("mynode").GetHtmlFromXElement();

I get

<mynode>Text with link <a href=''>Test</a></mynode>

But I need

Text with link <a href=''>Test</a>

How to do this right?


Solution

  • This looks like roughly the same question, with some pretty in-depth answers.