Rest XML

AussieBoy

Well-known member
Joined
Sep 7, 2020
Messages
78
Programming Experience
Beginner
Hi, I have to create a REST program to consume XML.
Is RestSharp the best way to go? and then use XMLconvert?
If there are any tutorials to help me on the way. I would appreciate being made aware of them.
Also of a test API I can use for Rest XML please.
Thanks,
 
Although REST is data agnostic, if you are going with some as modern as REST, why go backwards by having the data be in XML? JSON is the preferred format for structured data.

Anyway, you shouldn't need anything extra like RestSharp or XMLConvert. The .NET Framework has the needed classes to make tht REST calls, and the XML parsing.
 
Although REST is data agnostic, if you are going with some as modern as REST, why go backwards by having the data be in XML? JSON is the preferred format for structured data.

Anyway, you shouldn't need anything extra like RestSharp or XMLConvert. The .NET Framework has the needed classes to make tht REST calls, and the XML parsing.
I have done an app calling Json. I have been asked to do an app calling XML. What are the names of the classes in the .NET frame work that can be used?
 
Hi, I have found a tutorial, that has got me so far.
However, if I put in xml as
<?xml version="1.0" encoding="UTF - 8"?>
< string xmlns = "ht tp://www.xyz.com/webservices/" >
< DRInfo >< DataOwner ID = "bc093a17-12e4-465b-8927-a9e40d57ec7b" Name = "XXX" Description = "" /> etc


string conString; conString = ""; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(""); if (xmlDoc.DocumentElement.Attributes["name:
 != null) conString = xmlDoc.DocumentElement.Attributes["name"].Value + "\r\n"; if (xmlDoc.DocumentElement.Attributes["age"] != null) conString = conString + xmlDoc.DocumentElement.Attributes["age"].Value + "\r\n"; textBox1.Text = conString;"]string conString;
            conString = "";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml("<user name=\"John Doe\" age=\"42\"></user>");
            if (xmlDoc.DocumentElement.Attributes["name"] != null)
                conString = xmlDoc.DocumentElement.Attributes["name"].Value + "\r\n";
            if (xmlDoc.DocumentElement.Attributes["age"] != null)
                conString = conString + xmlDoc.DocumentElement.Attributes["age"].Value + "\r\n";
            textBox1.Text = conString;
 
Hi,
from this and adding to it I have the below;
I am trying to dig out the "European Central Bank" from the xml response payload.
Can any one help please?
Thanks,

C#:
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2020-12-16">
<Cube currency="USD" rate="1.2189"/>
<Cube currency="JPY" rate="125.97"/>
<Cube currency="BGN" rate="1.9558"/>
<Cube currency="CZK" rate="26.200"/>
<Cube currency="DKK" rate="7.4415"/>
<Cube currency="GBP" rate="0.89950"/>
<Cube currency="HUF" rate="355.52"/>
<Cube currency="PLN" rate="4.4354"/>
<Cube currency="RON" rate="4.8688"/>
<Cube currency="SEK" rate="10.1785"/>


C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace XMLApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [XmlRoot(ElementName = "Sender", Namespace = "http://www.gesmes.org/xml/2002-08-01")]
        public class Sender
        {
            [XmlElement(ElementName = "name", Namespace = "http://www.gesmes.org/xml/2002-08-01")]
            public string Name { get; set; }
        }

        [XmlRoot(ElementName = "Cube", Namespace = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")]
        public class Cube
        {
            [XmlAttribute(AttributeName = "currency")]
            public string Currency { get; set; }
            [XmlAttribute(AttributeName = "rate")]
            public string Rate { get; set; }
        }

        [XmlRoot(ElementName = "Envelope", Namespace = "http://www.gesmes.org/xml/2002-08-01")]
        public class Envelope
        {
            [XmlElement(ElementName = "subject", Namespace = "http://www.gesmes.org/xml/2002-08-01")]
            public string Subject { get; set; }
            [XmlElement(ElementName = "Sender", Namespace = "http://www.gesmes.org/xml/2002-08-01")]
            public Sender Sender { get; set; }
            [XmlElement(ElementName = "Cube", Namespace = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")]
            public Cube Cube { get; set; }
            [XmlAttribute(AttributeName = "gesmes", Namespace = "http://www.w3.org/2000/xmlns/")]
            public string Gesmes { get; set; }
            [XmlAttribute(AttributeName = "xmlns")]
            public string Xmlns { get; set; }
        }



        private void button1_Click(object sender, EventArgs e)
        {
            string conString;
            conString = "";
            string Value;
            Value = "";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
            
            foreach (XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes[2].ChildNodes[0].ChildNodes)
                conString = conString + xmlNode.Attributes["currency"].Value + ": " + xmlNode.Attributes["rate"].Value + "\r\n";

            textBox1.Text = conString;
            textBox2.Text = xmlDoc.DocumentElement.LocalName;
        }
    }
}
 
In the code above you demonstrated that you already know how to navigate around the XML DOM to get the currency rates. Why not do the same to get the other values you are looking for?
 
In the code above you demonstrated that you already know how to navigate around the XML DOM to get the currency rates. Why not do the same to get the other values you are looking for?
Do you mean something like
C#:
foreach (XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes[1].ChildNodes[0].ChildNodes)
                conString1 = conString + xmlNode.Attributes["name"].Value;
            textBox2.Text = conString1;
I'm afraid I can't put together with what I have seen up to now. Its probably real simple, but I can't see it at the moment.
 
Back
Top Bottom