-2

I have a code like this

 var summary = member.Select(o => o.Element("summary"))
                .FirstOrDefault();

This gives the value like

<summary>
   Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1" />. 
</summary>

When i try to get the value from summary like summary.value it gives only the value

Gets the number of elements contained in the .

The part <see cref="T:System.Collections.Generic.ICollection1" />` in that string is not coming.

Please help in fixing this.

Captured on debugging.

Capture on debugging for reference

Summary value

UPDATE

I tried escaping as per comment

        var summary = member.Select(o => o.XPathSelectElement("summary").Value.Replace("&lt;", "<").Replace("&amp;", "&").Replace("&gt;", ">"))
            .FirstOrDefault();

Still same result. May be I am doing something wrong.

shanmugharaj
  • 3,814
  • 7
  • 42
  • 70
  • 3
    You should escape the value beforehand with `<` for `<` and `>` for `>` and `&` for `&` – Icepickle Aug 01 '16 at 07:33
  • How can i improve this question. Down voters please suggest. – shanmugharaj Aug 01 '16 at 07:45
  • You could indicate how your xml is being built, and if the xml content is already escaped (you could check if with notepad instead of a xml viewer if the file in question has the malformed xml (eg: `<` in text instead of `<`) – Icepickle Aug 01 '16 at 07:47
  • 1
    @shansfk you want to [get 'InnerXml' of the `XElement`](http://stackoverflow.com/questions/3793/best-way-to-get-innerxml-of-an-xelement) – har07 Aug 01 '16 at 08:00
  • @har07 Thanks for the link. I did the same in that answer. It worked. Thank u once again. At least i got an answer before the question got closed. :) – shanmugharaj Aug 01 '16 at 08:37

2 Answers2

2

The reason you get it like that is because the XDocument is recognizing it as an nested element. You should convert that element into plain text and then you'll have it.

Something like:

var element = document.Element("summary");
var value = element.Value;

value += string.Join(", ", document.Element("summary")
                                   .Descendants()
                                   .Where(x => x.NodeType == System.Xml.XmlNodeType.Element
                                   .Select(x => x.ToString()));

Obviously you can not use .Descendants() and use .Element() or add .FirstOrDefault() instead of working with the collection, but this is the general idea

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
1

As per this link I created a XmlReader object and read the content and get the inner xml. The code is:

public static string InnerXml(this XElement self)
{
    var reader = self.CreateReader();
    reader.MoveToContent();

    return reader.ReadInnerXml()?.Trim();
}
Community
  • 1
  • 1
shanmugharaj
  • 3,814
  • 7
  • 42
  • 70