2016-08-01 23 views
-2

我有這樣如何存取權限XML節點的值,如果它有「<」在它

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

代碼這給像

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

值當我試圖從價值總結如summary.value它只給出值

獲取包含在元素的數量。

的部分<see cref="T:System.Collections.Generic.ICollection 1" />`在該字符串不來了。

請在解決這個幫助。

抓獲調試。

Capture on debugging for reference

Summary value

UPDATE

我試圖逃離按評論

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

還是同樣的結果。可能是我做錯了什麼。

+3

你應該逃脫值事先與''<爲'<'和''>爲'>'和''&爲'&' – Icepickle

+0

如何改善這個問題。向下選民請建議。 – shanmugharaj

+0

你可以指出你的xml是如何構建的,如果xml內容已經被轉義了(你可以檢查是否用記事本而不是xml查看器,如果有問題的文件有格式錯誤的xml(例如:<< '<') – Icepickle

回答

1

按本link我創建了一個XmlReader對象並讀取內容並獲取內部xml。該代碼是:

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

    return reader.ReadInnerXml()?.Trim(); 
} 
2

你得到它的原因是因爲XDocument認識到它是一個嵌套元素。您應該將該元素轉換爲純文本,然後您就可以擁有它。

喜歡的東西:

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())); 

顯然,你不能使用.Descendants()和使用.Element()或添加.FirstOrDefault(),而不是與收集工作,但這是一般的想法

+0

感謝您回覆。 – shanmugharaj

+0

@shansfk - 它解決了嗎? :) –

+0

我提到了[此鏈接](http://stackoverflow.com/questions/3793/best-way-to-get-innerxml-of-an-xelement)由har07建議。這對我很有用 – shanmugharaj

相關問題