0

I need to create XML request like this:

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

I have problem with PosXML line. It works only when to use simply PosXML but gets error when it is PosXML version="7.2.0"

My code right now:

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

Any suggestions?

Rein Kannumäe
  • 31
  • 1
  • 1
  • 5
  • 1
    I’m not sure what you are asking. Do you want the `version="7.2.0"` to be included in your document? Then you need to add `new XAttribute("version", "7.2.0")` inside the `PosXML` element. – poke Apr 19 '18 at 09:25
  • 1
    You seem to have some trouble understanding XML. I can recommend you to read through this [simple tutorial](https://www.w3schools.com/xml/xml_attributes.asp) to get some basic knowledge of XML and its parts. – Pulle Apr 19 '18 at 09:29
  • https://stackoverflow.com/questions/2454390/how-to-add-attributes-to-an-element-using-linq-c – qxg Apr 19 '18 at 09:32

1 Answers1

2

You can use an XAttribute for that:

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

(As poke also pointed out)

H.J. Meijer
  • 379
  • 2
  • 13