【发布时间】:2014-02-26 14:54:12
【问题描述】:
已解决:问题是我的括号放错了位置。我有:
if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf"), Text);
代替:
if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf", Text));
我有一个字符串(在我的程序中称为 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>&lt;p _triv="1" style="text-align:left"&gt;&lt;span style="font-family:'Verdana',sans serif;font-size:11pt;color:#FFFFFF"&gt;This is another text block.&lt;/span&gt;&lt;/p&gt;</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>
【问题讨论】: