【问题标题】:Strange result when adding HTML string to XElement将 HTML 字符串添加到 XElement 时出现奇怪的结果
【发布时间】: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><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 />&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>

【问题讨论】:

    标签: c# xml xelement


    【解决方案1】:

    使用

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

    而不是

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

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

    【讨论】:

    • 啊!就是这样。括号放错地方了!非常感谢!
    【解决方案2】:

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

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

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

    供参考:Documentation for 2 parameter XElement constructor

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 2021-01-20
      相关资源
      最近更新 更多