已解決:問題是我的圓括號在錯誤的地方。我:將HTML字符串添加到XElement時出現奇怪的結果
if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf"), Text);
相反的:
if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf", Text));
我有一個字符串(稱爲文字,在我的程序),看起來像這樣:
<p _triv="1" style="text-align:left"><span style="font-family:'Verdana',sans serif;font-size:11pt;color:#FFFFFF">This is another text block.</span></p>
我需要添加這個確切的字符串到一個名爲rtf
的XElement。在我的節目,我做這樣的:
if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf"), Text);
我想結果看起來像這樣: <rtf><p _triv="1" style="text-align:left"><span style="font-family:'Verdana',sans serif;font-size:11pt;color:#FFFFFF">This is another text block.</span></p></rtf>
但是,運行我的程序後,結果如下所示(以在rtf
節點是畸形,影響以下節點:
<text bordertype="ridge" defaulttext="" id="18748" idbefore="1" labelobject="18357" nohtmlpreload="false" ontop="false" outline="true" parent="3" proportional="false">
<name>Text Block 2</name>
<lastupdated>1393440325</lastupdated>
<transitionin>
<transtype>32</transtype>
<delay>3</delay>
<speed>10</speed>
</transitionin>
<transitionout>
<transtype>32</transtype>
<delay>5</delay>
<speed>10</speed>
</transitionout>
<possize>
<point>
<x>370</x>
<y>467</y>
</point>
<size>
<cx>200</cx>
<cy>100</cy>
</size>
</possize>
<rtf /><p _triv="1" style="text-align:left"><span style="font-family:'Verdana',sans serif;font-size:11pt;color:#FFFFFF">This is another text block.</span></p><bordercolor />000000<outlinecolor />FFFFFF<bordersize />1<marginsize />2</text>
我是添加字符串到節點錯誤爲了清楚起見,這裏是我的整個方法,我想生成的XML有:
public XElement CreateNode()
{
var xElement = new XElement("text");
if (BorderType != null) xElement.Add(new XAttribute("bordertype", BorderType));
if (DefaultText != null) xElement.Add(new XAttribute("defaulttext", DefaultText));
if (LabelObject != null) xElement.Add(new XAttribute("labelobject", LabelObject));
if (Outline != null) xElement.Add(new XAttribute("outline", Outline));
if (SchemaVersion != null) xElement.Add(new XAttribute("schemaver", SchemaVersion));
if (TextBlockType != null) xElement.Add(new XAttribute("textblocktype", TextBlockType));
if (ShowVerticalScrollBar == true) xElement.Add(new XAttribute("verticalscroll", ShowVerticalScrollBar));
base.CreateNode(ref xElement);
if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf"), Text);
if (BorderColor != null) xElement.Add(new XElement("bordercolor"), BorderColor);
if (OutlineColor != null) xElement.Add(new XElement("outlinecolor"), OutlineColor);
if (BorderSize != 0) xElement.Add(new XElement("bordersize"), BorderSize);
if (MarginSize != 0) xElement.Add(new XElement("marginsize"), MarginSize);
// This routine will sort the XAttributes of the XElement
var xdoc = new XDocument();
xdoc.Add(xElement);
Interface.Sort(xdoc);
return xElement;
}
預期結果:
<text bordertype="ridge" defaulttext="" id="18748" idbefore="1" labelobject="18357" nohtmlpreload="false" ontop="false" outline="true" parent="3" proportional="false">
<name>Text Block 2</name>
<lastupdated>1393440325</lastupdated>
<transitionin>
<transtype>32</transtype>
<delay>3</delay>
<speed>10</speed>
</transitionin>
<transitionout>
<transtype>32</transtype>
<delay>5</delay>
<speed>10</speed>
</transitionout>
<possize>
<point>
<x>370</x>
<y>467</y>
</point>
<size>
<cx>200</cx>
<cy>100</cy>
</size>
</possize>
<rtf><p _triv="1" style="text-align:left"><span style="font-family:'Verdana',sans serif;font-size:11pt;color:#FFFFFF">This is another text block.</span></p></rtf>
<bordercolor>000000</bordercolor>
<outlinecolor>FFFFFF</outlinecolor>
<bordersize>1</bordersize>
<marginsize>2</marginsize>
</text>
而且,我試着這樣做:
if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf"), XElement.Parse(Text));
但結果仍然關閉(請注意,rtf
格式不正確):
<text bordertype="ridge" defaulttext="" id="18748" idbefore="1" labelobject="18357" nohtmlpreload="false" ontop="false" outline="true" parent="3" proportional="false">
<name>Text Block 2</name>
<lastupdated>1393440325</lastupdated>
<transitionin>
<transtype>32</transtype>
<delay>3</delay>
<speed>10</speed>
</transitionin>
<transitionout>
<transtype>32</transtype>
<delay>5</delay>
<speed>10</speed>
</transitionout>
<possize>
<point>
<x>370</x>
<y>467</y>
</point>
<size>
<cx>200</cx>
<cy>100</cy>
</size>
</possize>
<rtf />
<p _triv="1" style="text-align:left">
<span style="font-family:'Verdana',sans serif;font-size:11pt;color:#FFFFFF">This is another text block.</span>
</p>
<bordercolor />000000<outlinecolor />FFFFFF<bordersize />1<marginsize />2</text>
唉!就是這樣。圓括號在錯誤的地方!非常感謝! – Kevin