【问题标题】:Xelement to expanded empty tagsXelement 扩展空标签
【发布时间】:2011-12-26 11:11:22
【问题描述】:

我在我的 c# 中生成 XML,例如,当我有几个空标签时,

 new XElement("TransLogID", "")

其中一些被渲染为

<TransLogID></TransLogID>

虽然其中一些被渲染为

<TransLogID/>

什么控制标签何时展开,何时不展开?我怎样才能强迫他们做出我想要的行为?

【问题讨论】:

    标签: c# .net xml sharepoint xelement


    【解决方案1】:

    我认为它们有不同的起源。

    Root.Add(new XElement("TransLogID1", ""));
    Root.Add(new XElement("TransLogID2"));
    

    会给

    <TransLogID1></TransLogID1>
    <TransLogID2/>
    

    两个元素都有空的 Elements/Nodes 集合,细微的区别在于 TransLogID2 的 IsEmpty=true。

    【讨论】:

      【解决方案2】:

      如果您的内容是空字符串 (new XElement("TransLogID", "")),它将呈现为

      <TransLogID></TransLogID>
      

      但如果为空(new XElement("TransLogID", null)),则会呈现为

      <TransLogID/>
      

      你确定你总是以同样的方式生成节点吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-03
        • 2020-09-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-04
        • 2015-03-07
        相关资源
        最近更新 更多