РЕШЕНО: проблема заключалась в том, что у меня была скобка не в том месте. Я имел:
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>
Мне нужно добавить именно эту строку в XElement с именем rtf
. В моей программе я делаю это так:
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>