Question

I have been trying to get the Wolframalpha API for C# working to no avail. I have been trying to use these two resources:

  1. Stack Question
  2. Wolfram API demos

The answer in the post was semi helpful but I can't get anything to compile. I'm new to C# so its a bit overwhelming. I am really having trouble trying to just get it to accept input and then output the result.

If anyone could either help me get this code working so I can work with a valid example or knows of an example project that I can model from it would be appreciated.

This is the code I cut and pasted into a C# (Visual Studio) console project:

    namespace WolframAlpha {
    using System;
    using System.Collections.Generic;
    using System.Data.Services.Client;
    using System.Net;
    using System.IO;


    public partial class DefaultPodEntity {

        private String _PlainText;

        private String _Img;

        private String _Title;

        private String _ParentTitle;

        private Int16 _ParentPosition;

        private String _ParentId;

        public String PlainText {
            get {
                return this._PlainText;
            }
            set {
                this._PlainText = value;
            }
        }

        public String Img {
            get {
                return this._Img;
            }
            set {
                this._Img = value;
            }
        }

        public String Title {
            get {
                return this._Title;
            }
            set {
                this._Title = value;
            }
        }

        public String ParentTitle {
            get {
                return this._ParentTitle;
            }
            set {
                this._ParentTitle = value;
            }
        }

        public Int16 ParentPosition {
            get {
                return this._ParentPosition;
            }
            set {
                this._ParentPosition = value;
            }
        }

        public String ParentId {
            get {
                return this._ParentId;
            }
            set {
                this._ParentId = value;
            }
        }
    }

    public partial class HtmlPodEntity {

        private String _Markup;

        private String _Title;

        private Int16 _Position;

        private String _Id;

        private String _Css;

        private String _Scripts;

        public String Markup {
            get {
                return this._Markup;
            }
            set {
                this._Markup = value;
            }
        }

        public String Title {
            get {
                return this._Title;
            }
            set {
                this._Title = value;
            }
        }

        public Int16 Position {
            get {
                return this._Position;
            }
            set {
                this._Position = value;
            }
        }

        public String Id {
            get {
                return this._Id;
            }
            set {
                this._Id = value;
            }
        }

        public String Css {
            get {
                return this._Css;
            }
            set {
                this._Css = value;
            }
        }

        public String Scripts {
            get {
                return this._Scripts;
            }
            set {
                this._Scripts = value;
            }
        }
    }

    public partial class PlainTextPodEntity {

        private String _PlainText;

        private String _Title;

        private String _ParentTitle;

        private Int16 _ParentPosition;

        private String _ParentId;

        public String PlainText {
            get {
                return this._PlainText;
            }
            set {
                this._PlainText = value;
            }
        }

        public String Title {
            get {
                return this._Title;
            }
            set {
                this._Title = value;
            }
        }

        public String ParentTitle {
            get {
                return this._ParentTitle;
            }
            set {
                this._ParentTitle = value;
            }
        }

        public Int16 ParentPosition {
            get {
                return this._ParentPosition;
            }
            set {
                this._ParentPosition = value;
            }
        }

        public String ParentId {
            get {
                return this._ParentId;
            }
            set {
                this._ParentId = value;
            }
        }
    }

    public partial class WolframAlphaFactsContainer : System.Data.Services.Client.DataServiceContext {

        public WolframAlphaFactsContainer(Uri serviceRoot) : 
                base(serviceRoot) {
        }

        /// <summary>
        /// </summary>
        /// <param name="Input">Query string Sample Values : weather|msft|derivative of x^4 sin x|SAT scores</param>
        /// <param name="Location">Location used for computation Sample Values : Madrid|Springfield, IL</param>
        /// <param name="LatitudeLongitude">Latitude/Longitude used for computation Sample Values : 40.42,-3.71|-22.54,-43.12</param>
        /// <param name="Width">Width in pixels for images returned Sample Values : 300|500</param>
        public DataServiceQuery<DefaultPodEntity> GetImageResults(String Input, String Location, String LatitudeLongitude, Int16? Width) {
            if ((Input == null)) {
                throw new System.ArgumentNullException("Input", "Input value cannot be null");
            }
            DataServiceQuery<DefaultPodEntity> query;
            query = base.CreateQuery<DefaultPodEntity>("GetImageResults");
            if ((Input != null)) {
                query = query.AddQueryOption("Input", string.Concat("\'", Input, "\'"));
            }
            if ((Location != null)) {
                query = query.AddQueryOption("Location", string.Concat("\'", Location, "\'"));
            }
            if ((LatitudeLongitude != null)) {
                query = query.AddQueryOption("LatitudeLongitude", string.Concat("\'", LatitudeLongitude, "\'"));
            }
            if (((Width != null) 
                        && (Width.HasValue == true))) {
                query = query.AddQueryOption("Width", Width.Value);
            }
            return query;
        }

        /// <summary>
        /// </summary>
        /// <param name="Input">Query string Sample Values : weather|msft|derivative of x^4 sin x|SAT scores</param>
        /// <param name="Location">Location used for computation Sample Values : Madrid|Springfield, IL</param>
        /// <param name="LatitudeLongitude">Latitude/Longitude used for computation Sample Values : 40.42,-3.71|-22.54,-43.12</param>
        /// <param name="Width">Width in pixels for images returned Sample Values : 300|500</param>
        public DataServiceQuery<HtmlPodEntity> GetHtmlResults(String Input, String Location, String LatitudeLongitude, Int16? Width) {
            if ((Input == null)) {
                throw new System.ArgumentNullException("Input", "Input value cannot be null");
            }
            DataServiceQuery<HtmlPodEntity> query;
            query = base.CreateQuery<HtmlPodEntity>("GetHtmlResults");
            if ((Input != null)) {
                query = query.AddQueryOption("Input", string.Concat("\'", Input, "\'"));
            }
            if ((Location != null)) {
                query = query.AddQueryOption("Location", string.Concat("\'", Location, "\'"));
            }
            if ((LatitudeLongitude != null)) {
                query = query.AddQueryOption("LatitudeLongitude", string.Concat("\'", LatitudeLongitude, "\'"));
            }
            if (((Width != null) 
                        && (Width.HasValue == true))) {
                query = query.AddQueryOption("Width", Width.Value);
            }
            return query;
        }

        /// <summary>
        /// </summary>
        /// <param name="Input">Query string Sample Values : weather|msft|derivative of x^4 sin x|SAT scores</param>
        /// <param name="Location">Location used for computation Sample Values : Madrid|Springfield, IL</param>
        /// <param name="LatitudeLongitude">Latitude/Longitude used for computation Sample Values : 40.42,-3.71|-22.54,-43.12</param>
        /// <param name="Width">Width in pixels for images returned Sample Values : 300|500</param>
        public DataServiceQuery<PlainTextPodEntity> GetPlainTextResults(String Input, String Location, String LatitudeLongitude, Int16? Width) {
            if ((Input == null)) {
                throw new System.ArgumentNullException("Input", "Input value cannot be null");
            }
            DataServiceQuery<PlainTextPodEntity> query;
            query = base.CreateQuery<PlainTextPodEntity>("GetPlainTextResults");
            if ((Input != null)) {
                query = query.AddQueryOption("Input", string.Concat("\'", Input, "\'"));
            }
            if ((Location != null)) {
                query = query.AddQueryOption("Location", string.Concat("\'", Location, "\'"));
            }
            if ((LatitudeLongitude != null)) {
                query = query.AddQueryOption("LatitudeLongitude", string.Concat("\'", LatitudeLongitude, "\'"));
            }
            if (((Width != null) 
                        && (Width.HasValue == true))) {
                query = query.AddQueryOption("Width", Width.Value);
            }
            return query;
        }
    }
}
Was it helpful?

Solution

This codeplex project claims to cover the latest Wolfram Alpha API and includes a sample: http://wolframalphaapi20.codeplex.com/

Console applications use a static Main method as their entry point. This routine can normally be found in a file program.cs that is created automatically when a new project for a console application is created.

If the compiler says it can't find Main then it probably was deleted or was never created. Difficult to say without any code to look at. More errors may show when the issue with the Main method was resolved.

OTHER TIPS

I am currently playing with a lib call WolframAlpha.NET. Code source is on github. There is a nuget package (Last published 2019-06-24).

Examples (from readme)

Here is the simplest form of getting data from Wolfram|Alpha:

static void Main(string[] args)
{
    //First create the main class:
    WolframAlpha wolfram = new WolframAlpha("APPID HERE");

    //Then you simply query Wolfram|Alpha like this
    //Note that the spelling error will be correct by Wolfram|Alpha
    QueryResult results = wolfram.Query("Who is Danald Duck?");

    //The QueryResult object contains the parsed XML from Wolfram|Alpha. Lets look at it.
    //The results from wolfram is split into "pods". We just print them.
    if (results != null)
    {
        foreach (Pod pod in results.Pods)
        {
            Console.WriteLine(pod.Title);
            if (pod.SubPods != null)
            {
                foreach (SubPod subPod in pod.SubPods)
                {
                    Console.WriteLine(subPod.Title);
                    Console.WriteLine(subPod.Plaintext);
                }
            }
        }
    }
}

For more examples, take a look at the WolframAlphaNet.Examples and WolframAlphaNet.Tests projects.

You have copy-pasted class definitions (like DefaultPodEntity and WolframAlphaFactsContainer) that allow you to interact with the Wolfram API, but you do not have a definition for the Main() function that defines what your program should be doing with those classes. You will need to add the method definition

static void Main(string[] args)
{
  // TODO: call methods of WolframAlphaFactsContainer
} 

to one of the classes (e.g. WolframAlphaFactsContainer or a new one, like Program, that is not listed in your question. Once this compiles, you need to replace the TODO comment with C# statements that specify how you are interacting with the WolframAlphaFactsContainer class (e.g. create an instance of that class and call its GetImageResults() method with the proper parameters).

Note: you will need to learn basic C# programming idioms before you can successfully tackle the problem of writing a working, correct program in C# that does what you want to do (as opposed to relying on other people's code).

Note: Read the documentation on Main() and how to pass command line parameters to your program (should you want to do that).

Note: the class WolframAlphaFactsContainer is marked partial, which means there might be other parts of this class (see documentation). If there are, you will need to include those in your code as well.

I know this post is old, but seeing as how it comes up in google near the top: https://wapiex.codeplex.com/

This is the wrapper I just finished. It includes much more than the other codeplex project. Feel free to use it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top