将HTML字符串添加到XElement时的结果很奇怪

时间:2014-02-26 14:54:13

标签: c# xml xelement

解决:问题是我的括号在错误的地方。我有:

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 />&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;<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>&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>
    <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>

2 个答案:

答案 0 :(得分:2)

使用

if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf", Text));

而不是

if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf"), Text);

请参阅:new XElement("rtf", Text)

答案 1 :(得分:0)

看起来Text变量中的字符串作为文本进入XElement,而不是XML。我相信你想这样做:

if (!String.IsNullOrEmpty(Text)) xElement.Add(new XElement("rtf", Text) );

将值作为参数传递给XElement构造函数。这应该把它放在XML中。

供参考:Documentation for 2 parameter XElement constructor