0

这就是我的字符串的样子

<?xml version="1.0" encoding="UTF-8"?>
<payment id="123456" date="2013-05-16 11:24:59.0">
  <refund id="1" amount="10000.0" reason="refund 1"/>
  <refund id="2" amount="1200.0" reason="refund 2"/>
  <refund id="3" amount="2252.0" reason="refund 3"/>
</payment>

使用以下代码将其转换为 xml 后:

XmlDocument xml = new XmlDocument();
xml.LoadXml(myString);

我的 xml 文档如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<payment id="123456" date="2013-05-16 11:24:59.0">
  <refund id="1" amount="10000.0" reason="refund 1" />
  <refund id="2" amount="1200.0" reason="refund 2" />
  <refund id="3" amount="2252.0" reason="refund 3" />
</payment>

注意到区别了吗?出于某种原因,所有“退款”元素在结束标记之前都有一个额外的空格。有人知道为什么会这样吗?我试图将 PreserveWhitespace 设置为 false,但没有帮助。

4

2 回答 2

0

XmlDoument 将 XML 内容转换为有效标记。意思是,那里应该有一个空格(这不是必须的,但它是标准的)。

您可以尝试将整个 XML 放在 1 行中,我星期五的结果将与此处相同..

于 2013-05-23T04:48:14.903 回答
0
This is because of the inline closing of the XML tag.

    <?xml version="1.0" encoding="UTF-8"?>
    <payment id="123456" date="2013-05-16 11:24:59.0"**>**
      <refund id="1" amount="10000.0" reason="refund 1"**/>**
      <refund id="2" amount="1200.0" reason="refund 2"/>
      <refund id="3" amount="2252.0" reason="refund 3"/>
    </payment>

付款最后有一个单独的结束标签,但退款是一个自动结束的元素。默认情况下,将在结束标记之前添加一个空格。如果你不想要间距,那么你必须像旧方法一样做

<?xml version="1.0" encoding="UTF-8"?>
        <payment id="123456" date="2013-05-16 11:24:59.0">
          <refund id="1" amount="10000.0" reason="refund 1">**</refund>**
          <refund id="2" amount="1200.0" reason="refund 2"></refund>
          <refund id="3" amount="2252.0" reason="refund 3"></refund>
        </payment>
于 2013-05-23T05:03:20.823 回答