Jump to content

Reading specific XML node

AC-3

Hi.

 

I want to be able to read only the first node with the data I need in an XML file. (Here is the file). I only need the data from the first.

Cube time="2020-03-13"

My problem is that I of course don't know what the actual name of the node would be at any given time as they update it at different intervals. I want to do this in C# .Net Core. Any code to be able to read the data would be awesome!

 

Help is much appreciated!

Link to comment
Share on other sites

Link to post
Share on other sites

so i spend some time trying to solve this problem "the right" way with using XmlSerializer and writting next to no code but it just threw invalid XML errors 247 :D

 

so i tried something a bit more "dirty" 

1) trime the XML file

i tried to remove everything except

Spoiler

image.png.276568678ba157ab54d6bdcb7068a14f.png

 

 

the black box is what i called firstCube

i tried to retrieve the time afterwards just to validate

 

then i used to regexp to get alle the currencies & rates

 

Spoiler

Date: 2020-03-03
Date: currency = USD, rate = 1.1157
Date: currency = JPY, rate = 117.76
Date: currency = BGN, rate = 1.9558
Date: currency = CZK, rate = 26.96
Date: currency = DKK, rate = 7.4731
Date: currency = GBP, rate = 0.90918
Date: currency = HUF, rate = 344.97
Date: currency = PLN, rate = 4.407
Date: currency = RON, rate = 4.8343
Date: currency = SEK, rate = 10.8998
Date: currency = CHF, rate = 1.0546
Date: currency = ISK, rate = 151.2
Date: currency = NOK, rate = 11.4765
Date: currency = HRK, rate = 7.5785
Date: currency = RUB, rate = 83.5905
Date: currency = TRY, rate = 7.1568
Date: currency = AUD, rate = 1.8198
Date: currency = BRL, rate = 5.5381
Date: currency = CAD, rate = 1.5539
Date: currency = CNY, rate = 7.8351
Date: currency = HKD, rate = 8.6682
Date: currency = IDR, rate = 16756.2
Date: currency = ILS, rate = 4.1795
Date: currency = INR, rate = 82.8075
Date: currency = KRW, rate = 1373.34
Date: currency = MXN, rate = 25.4478
Date: currency = MYR, rate = 4.8092
Date: currency = NZD, rate = 1.8499
Date: currency = PHP, rate = 57.625
Date: currency = SGD, rate = 1.5868
Date: currency = THB, rate = 35.937
Date: currency = ZAR, rate = 18.5602

 

the result isnt clean and i wouldnt use something like that in production

if i where you and needed to run this in prod somewhere i would try to get a valid xml file and just use serializer, its like 3 lines of code :D

you even can post XML inside visual studio and the programm will create the classes for you

 

Code: https://gist.github.com/masterholdy/75aeaf2e1da644c830270e12c63ada91

 

@Oliver24x if you have a question just quote me - sorry for bad english - if something wasnt clear i tried to re-write it

Link to comment
Share on other sites

Link to post
Share on other sites

Have you tried FirstChild of cube node? Get the first child then iterate it's contents.

 

https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.firstchild?view=netframework-4.8

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

I have the feeling you must be overthinking something as i don't see what is actually difficult here.

 

If you deserialize into a class object and get the Enveloppe.Cube property and order its children by Date descending it gives you what you need.

Link to comment
Share on other sites

Link to post
Share on other sites

You shouldn't use regex, just use some xml parsing library or whatever's built into the programming language.

You should have functions like FirstChild, NextChild (null when there's no more children so you could do something like while child != null  {  ,....,  child  = parent.nextChild() }  or something like that)  and you should have something like GetAttribute or GetProperty so you'd say  currency = node.getProperty('currency');  value = node.getProperty('rate');

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks to everyone helping out!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×