【问题标题】:c# White space in XElementc# XElement 中的空白
【发布时间】:2018-04-19 17:30:16
【问题描述】:

我需要像这样创建 XML 请求:

<PosXML version="7.2.0">
<ReadCardRequest>
<Amount>10</Amount>
<CurrencyName>EUR</CurrencyName>
</ReadCardRequest>
</PosXML>

我对 PosXML 行有疑问。它仅在仅使用 PosXML 时才有效,但在 PosXML version="7.2.0"

时会出错

我现在的代码:

XDocument doc = new XDocument(new XElement("PosXML",
                                          new XElement("ReadCardRequest",
                                              new XElement("Amount", summa.ToString()),
                                              new XElement("CurrencyName", "EUR"))));

有什么建议吗?

【问题讨论】:

  • 我不确定你在问什么。您希望version="7.2.0" 包含在您的文档中吗?然后你需要在PosXML元素内添加new XAttribute("version", "7.2.0")
  • 您在理解 XML 方面似乎有些困难。我可以推荐你通读这个simple tutorial 以了解 XML 及其组成部分的一些基本知识。

标签: c# xml


【解决方案1】:

您可以为此使用 XAttribute:

XDocument doc = new XDocument(new XElement("PosXML",
                                  new XElement("ReadCardRequest",
                                      new XElement("Amount", "1"),
                                           new XElement("CurrencyName", "EUR")),
                                  new XAttribute("version","7.2.0")));

(正如戳也指出)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多