如何将文本使用的XElement时 [英] How to put in text when using XElement
问题描述
我使用的是新的System.Xml.Linq的创建HTML文档(是的,我知道的HTMLDocument,但更喜欢的XDocument /的XElement类)。我在插入一个问题&放大器; NBSP;
(或任何其他HTML实体)。我已经试过:
-
只是把文字直接不因为与放大器工作;被翻INT和放大器;.
新的XElement(H1,文本和放大器; NBSP;到&安培; NBSP;保持和放大器; NBSP;合);
-
我试着用以下的原始XML解析,但与此错误barfs:
XElement.Parse(文字与NBSP; 。到 保持 合);
- >引用了未声明的实体nbsp'.`
-
尝试之三如下所示。如果我保存到一个文件中,只有一个空格,
&放大器;#160;
丢失
VAR X =新的XDocument(新的XElement(名称,同页(妈妈你好!)));
私有静态XNode同页(串P)`
{
返回XElement.Parse(< XML>中+ p.Replace(,&放大器;#160;)+ &所述; / xml的>中。)FirstNode;
}
我无法找到一个方法来刚刚经历推原始文本没有它越来越逃过一劫。我失去了一些东西明显?
我无法找到一种方法,只是推生。通过文字,没有它得到逃过
块引用>
只要把Unicode字符在&放大器; NBSP;是指直接在文本节点(U + 00A0 NO-BREAK SPACE),然后让这件事是否需要逃到&安培串行忧;#160;或不。 (也许不是:如果您使用UTF-8或ISO-8859-1的字符编码你的页面就可以直接列入,而不必担心其编码为实体引用或字符引用)
新的XElement(H1,Text\\\ to\\\ keep\\\ together);
I'm using the new System.Xml.Linq to create HTML documents (Yes, I know about HtmlDocument, but much prefer the XDocument/XElement classes). I'm having a problem inserting
(or any other HTML entity). What I've tried already:
Just putting text in directly doesn't work because the & gets turned int &.
new XElement("h1", "Text to keep together.");
I tried parsing in the raw XML using the following, but it barfs with this error:
XElement.Parse("Text to keep together.");
--> Reference to undeclared entity 'nbsp'.`
Try number three looks like the following. If I save to a file, there is just a space, the
 
gets lost.var X = new XDocument(new XElement("Name", KeepTogether("Hi Mom!"))); private static XNode KeepTogether(string p)` { return XElement.Parse("<xml>" + p.Replace(" ", " ") + "</xml>").FirstNode; }
I couldn't find a way to just shove the raw text through without it getting escaped. Am I missing something obvious?
解决方案I couldn't find a way to just shove the raw text through without it getting escaped.
Just put the Unicode character in that refers to (U+00A0 NO-BREAK SPACE) directly in a text node, then let the serializer worry about whether it needs escaping to   or not. (Probably not: if you are using UTF-8 or ISO-8859-1 as your page encoding the character can be included directly without having to worry about encoding it into an entity reference or character reference).
new XElement("h1", "Text\u00A0to\u00A0keep\u00A0together");
这篇关于如何将文本使用的XElement时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!