xml內容如下:
<xml>
<item content="abcd 
 abcd 
 abcd" />
</xml>
使用時XmlDocument要讀取的內容content屬性,
并
自動轉義。
代碼:
XmlDocument doc = new XmlDocument();
var content = doc.SelectSingleNode("/xml/item").Attributes["content"].Value;
如何在不轉義字符的情況下獲取原始文本?
uj5u.com熱心網友回復:
如果這些字符在沒有轉義的情況下被寫入詞法 XML 流,那么作為 XML 行結束規范化規則的結果,當接收者讀取流時,它們將被 XML 決議器吞掉。所以你弄錯了:它們被轉義的原因是為了保護它們;如果他們不逃脫,他們就會迷路。
uj5u.com熱心網友回復:
我有一個解決方法,它對我有用:
private static string GetAttributeValue(XmlNode node, string attributeName)
{
if (node == null || string.IsNullOrWhiteSpace(attributeName))
{
throw new ArgumentException();
}
const string CharLF = "
";
const string CharCR = "
";
string xmlContent = node.OuterXml;
if (!xmlContent.Contains(CharLF) && !xmlContent.Contains(CharCR))
{
// no special char, return its original value directly
return node.Attributes[attributeName].Value;
}
string value = string.Empty;
if (xmlContent.Contains(attributeName))
{
value = xmlContent.Substring(xmlContent.IndexOf(attributeName)).Trim();
value = value.Substring(value.IndexOf("\"") 1);
value = value.Substring(0, value.IndexOf("\""));
}
return value;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/367965.html
上一篇:JS序列中出現的次數是素數
