Jump to content

Help with some C# coding

UPDATE: 11/24/16
Happy thanksgiving :D and I have no idea how to delete this so I'll just leave this here: I HAVE ACCOMPLISHED EVERYTHING I NEEDED IN MY ORIGINAL POST SO DO NOT COMMENT PLEASE

 

UPDATE: 8/10/16

I started school so I'm not gonna have as much time to work on this. ;-;
 

UPDATE: 8/7/16

I fixed the Form1_Activated loop.

 

UPDATE: 8/5/16

 

I will keep trying to fix the annoying loop that's caused whenever the form is activated.

In the mean time I'll make a picture viewer similar to Windows Photo Viewer. I did this before but that was when I was using c# for the first time. Now I have a bit more knowledge and hopefully will improve it.

 

UPDATE: 8/3/16

Here's what I have accomplished so far:

 

1. Downloading an image from a URL

2. Read and be able to select certain roots of the XML file - thanks to Nuluvius

 

Here's what's left:

 

1. Close the XML file when a new or similar search is performed. There's no File.Close(); that I'm aware of. Also I'm using XDocument.Load(); to load the XML.

2. Being able to relocate the controls when the window is resized

3. Auto size the groupbox in case the the title is large and doesn't cut off(see the third picture)

 

I will also provide the source code and .exe file's here:

Updated: 8/7/16

Login: http://pastebin.com/94iLbkhF

Image_Browser: http://pastebin.com/aDsbwb97

.exe:  https://mega.nz/#!rAUEQK5C!xg9hjpKnGwMZrlQ4XjT6dkJ9VFhyPBllcAcutrdkJek

 

To login:

If you HAVE an Inkbunny account:

type in your information and login, as soon as a SID is received, the password is destroyed.

 

If you DON'T have one and do not wish to make one:

use "guest" for the username and leave the password blank. Note that users have the option to block submissions from guests.

 

I really hope links are allowed...

 

I've included pictures of the app, just thought you might want to look at it, unless you don't want too. 

 

Here's the original post:

 

*I'm just learning c# again as I haven't coded in a while, so don't make your answers too complicated please. Thanks.*

 

I have 3 question for you guys:

 

1. How would I download a picture from a website? Ex. Image

2. How to read the values that I want from an xml output from a web server?

3. I want to access a variable thats in Form1(Login) from Form2(Image_Browser), how would I do this?

 

Here's my code: http://pastebin.com/94iLbkhF

Feel free to improve it, but don't steal it and claim it as your own. .-.

 

I want to be able to get only the name of the submission, the account title, the thumbnail of the submission, the rating of the submission such as "Mature," and the full screen URL of the image from the XML output of the server, everything else is useless for me right now. I have tried nothing yet as I'm still a newbie with c#.

 

Thanks!

 

Here's a sample of the XML from a web server:

 

<submission>
<submission_id>...</submission_id>
<hidden>...</hidden>
<username>
<![CDATA[ Kinaj ]]>
</username>
<user_id>...</user_id>
<create_datetime>...</create_datetime>
<create_datetime_usertime>...</create_datetime_usertime>
<last_file_update_datetime>...</last_file_update_datetime>
<last_file_update_datetime_usertime>...</last_file_update_datetime_usertime>
<thumbnail_url_huge_noncustom>...</thumbnail_url_huge_noncustom>
<thumbnail_url_large_noncustom>...</thumbnail_url_large_noncustom>
<thumbnail_url_medium_noncustom>
<![CDATA[
https://inkbunny.net/thumbnails/medium/1609/1609080_Kinaj_alex_marie_schlie__die_t_r_noncustom.jpg
]]>
</thumbnail_url_medium_noncustom>
<thumb_medium_noncustom_x>...</thumb_medium_noncustom_x>
<thumb_medium_noncustom_y>...</thumb_medium_noncustom_y>
<thumb_large_noncustom_x>...</thumb_large_noncustom_x>
<thumb_large_noncustom_y>...</thumb_large_noncustom_y>
<thumb_huge_noncustom_x>...</thumb_huge_noncustom_x>
<thumb_huge_noncustom_y>...</thumb_huge_noncustom_y>
<file_name>...</file_name>
<title>
<![CDATA[ Close the Door ]]>
</title>
<deleted>...</deleted>
<public>...</public>
<mimetype>...</mimetype>
<pagecount>...</pagecount>
<rating_id>...</rating_id>
<rating_name>
<![CDATA[ Mature ]]>
</rating_name>
<file_url_full>
<![CDATA[
https://inkbunny.net/files/full/1609/1609080_Kinaj_alex_marie_schlie__die_t_r.png
]]>
</file_url_full>
<file_url_screen>...</file_url_screen>
<file_url_preview>...</file_url_preview>
<submission_type_id>...</submission_type_id>
<type_name>...</type_name>
<digitalsales>...</digitalsales>
<printsales>...</printsales>
<friends_only>...</friends_only>
<guest_block>...</guest_block>
<scraps>...</scraps>
</submission>

 

Untitled.png

Untitled2.png

Untitled3.png

Edited by Jose Marquez
Post no longer valid
Link to comment
Share on other sites

Link to post
Share on other sites

I had this from computing science degree I just completed. I uses visual basic but other than slight syntax changes it's the same.

 

 

Manipulating XML for Web Services.pdf

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

On 31/07/2016 at 5:48 PM, Jose Marquez said:

2. How to read the values that I want from an xml output from a web server?

This is trivial using Linq to XML, have a look at the XDocument class.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Nuluvius said:

This is trivial using Linq to XML, have a look at the XDocument class.

 

6 hours ago, vorticalbox said:

I had this from computing science degree I just completed. I uses visual basic but other than slight syntax changes it's the same.

 

 

Manipulating XML for Web Services.pdf

I'll check these out. Thanks. :3

 

Update: I'm such a newbie that I don't understand any of the sources. :/

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, Jose Marquez said:

Update: I'm such a newbie that I don't understand any of the sources. :/

Given the following XML fragment:

<?xml version="1.0" encoding="utf-8" ?>
<Ponies>
  <Pony>
    <Name>Twilight Sparkle</Name>
    <Type>Alicorn</Type>
  </Pony>
  <Pony>
    <Name>Rainbow Dash</Name>
    <Type>Pegasus</Type>
  </Pony>
  <Pony>
    <Name>Fluttershy</Name>
    <Type>Pegasus</Type>
  </Pony>
  <Pony>
    <Name>Rarity</Name>
    <Type>Unicorn</Type>
  </Pony>
  <Pony>
    <Name>Applejack</Name>
    <Type>Earth</Type>
  </Pony>
  <Pony>
    <Name>Pinkie Pie</Name>
    <Type>Earth</Type>
  </Pony>
</Ponies>

Here's a simple example:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var document = XDocument.Load("Ponies.xml");
            Console.WriteLine(document);
            Console.WriteLine(Environment.NewLine);

            foreach (var pony in document.Descendants("Pony"))
            {
                Console.WriteLine($"Name: {pony.Element("Name").Value} Type: {pony.Element("Type").Value}");
            }

            Console.ReadKey();
        }
    }
}

 

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Nuluvius said:

Given the following XML fragment:


<?xml version="1.0" encoding="utf-8" ?>
<Ponies>
  <Pony>
    <Name>Twilight Sparkle</Name>
    <Type>Alicorn</Type>
  </Pony>
  <Pony>
    <Name>Rainbow Dash</Name>
    <Type>Pegasus</Type>
  </Pony>
  <Pony>
    <Name>Fluttershy</Name>
    <Type>Pegasus</Type>
  </Pony>
  <Pony>
    <Name>Rarity</Name>
    <Type>Unicorn</Type>
  </Pony>
  <Pony>
    <Name>Applejack</Name>
    <Type>Earth</Type>
  </Pony>
  <Pony>
    <Name>Pinkie Pie</Name>
    <Type>Earth</Type>
  </Pony>
</Ponies>

Here's a simple example:


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var document = XDocument.Load("Ponies.xml");
            Console.WriteLine(document);
            Console.WriteLine(Environment.NewLine);

            foreach (var pony in document.Descendants("Pony"))
            {
                Console.WriteLine($"Name: {pony.Element("Name").Value} Type: {pony.Element("Type").Value}");
            }

            Console.ReadKey();
        }
    }
}

 

I'll try to make this work in my application. Thanks. c:

 

Funny how I couldn't understand what's on Microsoft's website but I could understand this. :P

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, Jose Marquez said:

1. How would I download a picture from a website? Ex. Image

2. [...]

3. I want to access a variable thats in Form1(Login) from Form2(Image_Browser), how would I do this?

 

Here's my code: http://pastebin.com/94iLbkhF

Feel free to improve it, but don't steal it and claim it as your own. . .

 

To address your other questions:

1. To download a file and save it to the user's desktop you could use:

string url = "https://www.google.com/logos/doodles/2015/googles-new-logo-5078286822539264.3-hp2x.gif";
string savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), url.Split('/').Last());
using(var wc = new WebClient())
{
	wc.DownloadFile(url, savePath);
}

You can specify the savePath to whatever you want / need ofc. If you want to keep it as byte[] in memory you could simply use:

string url = "https://www.google.com/logos/doodles/2015/googles-new-logo-5078286822539264.3-hp2x.gif";
byte[] imageData;
using (var wc = new WebClient())
{
	imageData = wc.DownloadData(url);
}

Keep in mind to wrap the WebClient or the actual call in a try/catch block in case it can't connect to given ressource.

2. Accessing data from Multiple Forms can be achieved in many ways.

The easiest would be to declare the variables as properties and give each Form a property of type Form in wich a reference to the other form is stored.

public class Form1 : Form
{
	private Form _otherForm;
	
	public Form1()
	{
		// ...
	}

	public Form OtherForm
	{
		get { return _otherForm; }
		set
		{
			if(_otherForm == value) return;
			_otherForm = value;
		}
	}
}

public class Form2 : Form
{
	private Form _otherForm;
	
	public Form2()
	{
		// ...
	}

	public Form OtherForm
	{
		get { return _otherForm; }
		set
		{
			if(_otherForm == value) return;
			_otherForm = value;
		}
	}
}

public class Program
{
	public static void Main(string[] args)
	{
		Form1 f1 = new Form1();
		Form2 f2 = new Form2();
		f1.OtherForm = f2;
		f2.OtherForm = f1;
		// ...
	}
}

This will let each Form know about the other one and they can access each others Properties.

It can and most likely will create a huge mess when we start talkin' about more than one Thread running at the same time, so I strongly reccommend against that.

The more elegant solution is a thread-safe collection to store the data like it is described here or use cross-thread invokation, both of wich are relatively complex topics.

I reccommend starting out with a thread-safe collection of some kind when it comes to access to the data itself.

Link to comment
Share on other sites

Link to post
Share on other sites

Jirajha: I didn't know where to put these so I tried putting the block into one form and it just gave build errors so I gave up and I'll just save the variable to a file and access it from there. After messing with the variables I got it to download a file without Windows Photo Viewer saying that its corrupted. I'm also trying out 

 

Nuluvius: I'm trying out the code and it actually works without giving errors, I'll try to adapt this to work with what I need.

 

Thanks again guys. c:

Link to comment
Share on other sites

Link to post
Share on other sites

Now I'm getting this error:

 

An unhandled exception of type 'System.NullReferenceException' occurred in Inkbunny.exe

Additional information: Object reference not set to an instance of an object.

 

Here's where the error occurs:

 

private void button2_Click(object sender, EventArgs e)
        {
            var path = @"C:\Users\Blunt\Documents\Visual Studio 2015\Projects\Inkbunny\Inkbunny.xml";
            var document = XDocument.Load(path);
            foreach (var sub in document.Descendants("title"))
            {
               MessageBox.Show($"Name: {sub.Element("title").Value}");
            }
        }

 

Here's the XML file I'm using for testing:

 

<submission>
<submission_id>
	<![CDATA[ 1158775 ]]>
</submission_id>
<hidden>
	<![CDATA[ f ]]>
</hidden>
<username>
	<![CDATA[ PinkSyrup ]]>
</username>
<user_id>
		<![CDATA[ 331649 ]]>
</user_id>
<create_datetime>
	<![CDATA[ 2016-08-01 17:10:53.135638+02 ]]>
</create_datetime>
<create_datetime_usertime>
	<![CDATA[ 01 Aug 2016 08:10 PDT ]]>
</create_datetime_usertime>
<last_file_update_datetime>
	<![CDATA[ 2016-08-01 16:38:16.850678+02 ]]>
</last_file_update_datetime>
<last_file_update_datetime_usertime>
	<![CDATA[ 01 Aug 2016 07:38 PDT ]]>
</last_file_update_datetime_usertime>
<thumbnail_url_huge>
	<![CDATA[
	https://inkbunny.net/thumbnails/huge/1610/1610233_PinkSyrup_lauirbadenda.jpg
	]]>
</thumbnail_url_huge>
<thumbnail_url_large>
<![CDATA[
	https://inkbunny.net/thumbnails/large/1610/1610233_PinkSyrup_lauirbadenda.jpg
]]>
</thumbnail_url_large>
<thumbnail_url_medium>
<![CDATA[
	https://inkbunny.net/thumbnails/medium/1610/1610233_PinkSyrup_lauirbadenda.jpg
]]>
</thumbnail_url_medium>
<thumb_huge_x>
	<![CDATA[ 300 ]]>
</thumb_huge_x>
<thumb_huge_y>
	<![CDATA[ 300 ]]>
</thumb_huge_y>
<thumb_large_x>
	<![CDATA[ 200 ]]>
</thumb_large_x>
<thumb_large_y>
	<![CDATA[ 200 ]]>
</thumb_large_y>
<thumb_medium_x>
	<![CDATA[ 120 ]]>
</thumb_medium_x>
<thumb_medium_y>
	<![CDATA[ 120 ]]>
</thumb_medium_y>
<thumbnail_url_huge_noncustom>
	<![CDATA[
	https://inkbunny.net/files/preview/1610/1610233_PinkSyrup_lauirbadenda.jpg
]]>
</thumbnail_url_huge_noncustom>
<thumbnail_url_large_noncustom>
<![CDATA[
	https://inkbunny.net/thumbnails/large/1610/1610233_PinkSyrup_lauirbadenda_noncustom.jpg
]]>
</thumbnail_url_large_noncustom>
<thumbnail_url_medium_noncustom>
	<![CDATA[
	https://inkbunny.net/thumbnails/medium/1610/1610233_PinkSyrup_lauirbadenda_noncustom.jpg
	]]>
</thumbnail_url_medium_noncustom>
<thumb_medium_noncustom_x>
	<![CDATA[ 120 ]]>
</thumb_medium_noncustom_x>
<thumb_medium_noncustom_y>
	<![CDATA[ 120 ]]>
</thumb_medium_noncustom_y>
<thumb_large_noncustom_x>
	<![CDATA[ 200 ]]>
</thumb_large_noncustom_x>
<thumb_large_noncustom_y>
	<![CDATA[ 200 ]]>
</thumb_large_noncustom_y>
<thumb_huge_noncustom_x>
	<![CDATA[ 300 ]]>
</thumb_huge_noncustom_x>
<thumb_huge_noncustom_y>
	<![CDATA[ 300 ]]>
</thumb_huge_noncustom_y>
<file_name>
		<![CDATA[ 1610233_PinkSyrup_lauirbadenda.png ]]>
</file_name>
<title>
	<![CDATA[ Kitten Pit ]]>
</title>
<deleted>
	<![CDATA[ f ]]>
</deleted>
<public>
	<![CDATA[ t ]]>
</public>
<mimetype>
	<![CDATA[ image/png ]]>
</mimetype>
<pagecount>
	<![CDATA[ 10 ]]>
</pagecount>
<rating_id>
	<![CDATA[ 2 ]]>
</rating_id>
<rating_name>
	<![CDATA[ Adult ]]>
</rating_name>
<file_url_full>
	<![CDATA[
	https://inkbunny.net/files/full/1610/1610233_PinkSyrup_lauirbadenda.png
	]]>
</file_url_full>
<file_url_screen>
	<![CDATA[
	https://inkbunny.net/files/screen/1610/1610233_PinkSyrup_lauirbadenda.png
	]]>
</file_url_screen>
<file_url_preview>
	<![CDATA[
	https://inkbunny.net/files/preview/1610/1610233_PinkSyrup_lauirbadenda.jpg
	]]>
</file_url_preview>
<submission_type_id>
	<![CDATA[ 1 ]]>
</submission_type_id>
<type_name>
	<![CDATA[ Picture/Pinup ]]>
</type_name>
<digitalsales>
	<![CDATA[ f ]]>
</digitalsales>
<printsales>
	<![CDATA[ f ]]>
</printsales>
<friends_only>
	<![CDATA[ f ]]>
</friends_only>
<guest_block>
	<![CDATA[ t ]]>
</guest_block>
<scraps>
	<![CDATA[ f ]]>
</scraps>
</submission>

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 01/08/2016 at 3:44 PM, Jose Marquez said:

Funny how I couldn't understand what's on Microsoft's website but I could understand this. :P

Obviously that's because MSDN does not use ponies.

On 31/07/2016 at 5:48 PM, Jose Marquez said:

3. I want to access a variable thats in Form1(Login) from Form2(Image_Browser), how would I do this?

Essentially as @Jirajha demonstrated. What you are doing is Dependency Injection (DI); you are passing a reference to another instance of something into your class. This is best accomplished using cleanly defined interfaces such that you can avoid specific coupling and also so that you can substitute the implementation at any point in the future i.e when testing your boundaries. I'd suggest that you should read up on Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion (SOLID); which are a set of five very important core Software Engineering principles.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, Nuluvius said:

Obviously that's because MSDN does not use Ponies.

Essentially as @Jirajha demonstrated. What you are doing is Dependency Injection (DI); you are passing a reference to another instance of something into your class. This is best accomplished using cleanly defined interfaces such that you can avoid specific coupling and also so that you can substitute the implementation at any point in the future i.e when testing your boundaries. I'd suggest that you should read up on Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion (SOLID); which are a set of five very important core Software Engineering principles.

True. :P

 

I lost it at Dependency Injection... I'll read the wikipedia articles and try to understand...

Link to comment
Share on other sites

Link to post
Share on other sites

On 01/08/2016 at 6:04 PM, Jose Marquez said:

Now I'm getting this error:

 

An unhandled exception of type 'System.NullReferenceException' occurred in Inkbunny.exe

Additional information: Object reference not set to an instance of an object.

Looks like you're not drilling down far enough, try this code:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var document = XDocument.Load("SomeXml.xml");

            Console.WriteLine(document);
            Console.WriteLine(Environment.NewLine);

            foreach (var submission in document.Descendants("submission"))
            {
                Console.WriteLine(submission.Element("title"));
            }

            Console.ReadKey();
        }
    }
}

You may want to start thinking about building in some intelligence about handling elements that perhaps don't exist. The Submission XML element seems to suggest that you could deserialize it into a class.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

I updated the post, it has the things that I would like to accomplish at this time. More things may pop up.

 

 

 

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

×