0

I am writing a method in c# that gets xml from web service with a lot of data that I do not need. I managed to select only tags that I need with this command: var exchangeRateDataSet = xmlNew.Descendants("ExchangeRateDataSet");

Now I need to convert it to string so I can deserialize it and convert it to object of my class.

But, there is a problem. When i try to do this: var toRead = exchangeRateDataSet.ToString();

I get this as a string, which of course isn't valid: System.Xml.Linq.XContainer+d__39

This is a piece of xml that I have:

``
<?xml version="1.0"?>
<ExchangeRateDataSet xmlns="">
<ExchangeRate xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" msdata:rowOrder="0" diffgr:id="ExchangeRate1">
<ExchangeRateListNumber>111</ExchangeRateListNumber>
<Date>14.06.2019</Date>
<CreateDate>14.06.2019</CreateDate>
<DateTo>31.12.4172</DateTo>
<ExchangeRateListTypeID>1</ExchangeRateListTypeID>
<CurrencyGroupID>2</CurrencyGroupID>
<CurrencyCode>978</CurrencyCode>
<CurrencyCodeNumChar>978</CurrencyCodeNumChar>
<CurrencyCodeAlfaChar>EUR</CurrencyCodeAlfaChar>
<CurrencyNameSerCyrl>Евро</CurrencyNameSerCyrl>
<CurrencyNameSerLat>Evro</CurrencyNameSerLat>
<CurrencyNameEng>Euro</CurrencyNameEng>
<CountryNameSerCyrl>ЕМУ</CountryNameSerCyrl>
<CountryNameSerLat>EMU</CountryNameSerLat>
<CountryNameEng>EMU</CountryNameEng>
<Unit>1</Unit>
<BuyingRate>117.6265</BuyingRate>
<MiddleRate>0.0000</MiddleRate>
<SellingRate>118.3343</SellingRate>
<FixingRate>0.000000</FixingRate>
</ExchangeRate>
<ExchangeRate xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" msdata:rowOrder="1" diffgr:id="ExchangeRate2">
<ExchangeRateListNumber>111</ExchangeRateListNumber>
<Date>14.06.2019</Date>
<CreateDate>14.06.2019</CreateDate>
<DateTo>31.12.4172</DateTo>
<ExchangeRateListTypeID>1</ExchangeRateListTypeID>
<CurrencyGroupID>3</CurrencyGroupID>
<CurrencyCode>36</CurrencyCode>
<CurrencyCodeNumChar>036</CurrencyCodeNumChar>
<CurrencyCodeAlfaChar>AUD</CurrencyCodeAlfaChar>
<CurrencyNameSerCyrl>Аустралијски долар</CurrencyNameSerCyrl>
<CurrencyNameSerLat>Australijski dolar</CurrencyNameSerLat>
<CurrencyNameEng>Australian Dollar</CurrencyNameEng>
<CountryNameSerCyrl>Аустралија</CountryNameSerCyrl>
<CountryNameSerLat>Australija</CountryNameSerLat>
<CountryNameEng>Australia</CountryNameEng>
<Unit>1</Unit>
<BuyingRate>71.9868</BuyingRate>
<MiddleRate>0.0000</MiddleRate>
<SellingRate>72.4200</SellingRate>
<FixingRate>0.000000</FixingRate>
</ExchangeRate>
``

To summarize, my question is how to convert this xml to object? Any help would be appreciated. Thanks.

dj.milojevic
  • 204
  • 1
  • 5
  • 12
  • 1
    You do not have a well formed xml because you root level is an array of elements and the root level of an xml can have only one element. You are taking the wrong approach. Flattening out an xml using descendants looses the hierarchy of the data which you will need to put object into classes. I usually first create the classes, then using xml linq parse data directly into the class and skip the serialize code. See : https://stackoverflow.com/questions/56242787/iterate-on-xml-with-lots-of-sales/56243894#56243894 – jdweng Jun 14 '19 at 10:00
  • Thanks, this did the trick with some modifications. We can consider your response as a right one. – dj.milojevic Jun 14 '19 at 12:21
  • You can use Serialize and Deserialize classes, and use Activator.CreateInstance to convert xml to object. – Milad Hatami Jun 15 '19 at 09:17
  • Your query returns an enumerable of `XElement` objects. You can deserialize each one directly using `XmlSerializer` by using `XElement.CreateReader()` as shown in [`Deserialize XElement into Class(s)`](https://stackoverflow.com/q/6311612) or [Deserialize two separate objects from one XML file](https://stackoverflow.com/q/29017374). In fact this may be a duplicate, agree? – dbc Jun 17 '19 at 00:47
  • There was no need for deserialization at all. There is a much simpler way, see @jdweng 's comment. – dj.milojevic Jun 17 '19 at 07:58

0 Answers0