'SyndicationFeed' does not contain a definition for 'Load' - c#

I'm trying to read rss feeds using Syndicationfeed class.
I have added a reference to System.servicemodel.syndication.
this is my error Project.SyndicationFeed' does not contain a definition for 'Load'
Here's my code: (console application)
using System;
using System.Xml;
using System.ServiceModel.Syndication;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string url = "http://fooblog.com/feed";
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = new SyndicationFeed();
feed = SyndicationFeed.Load(reader);
reader.Close();
foreach (SyndicationItem item in feed.Items)
{
String subject = item.Title.Text;
String summary = item.Summary.Text;
}
}
}
}

The problem was that somehow a class SyndicationFeed.cs got added to my project which caused conflicts when calling the .Load() method.
After deleting this file from the class everything went fine.
Thanks to #user2864740 for pointing this out and leading me to the solution.

Related

Loading from an Xml file

Okay so this is very basic but I've literally started learning how to read an XML document today and i usually find answers more comprehensive on here than on online guides. Essentially i'm coding a Pokemon game which uses an XML file to load all the stats (its one i swiped from someone else).The user will input a Pokemon and i then want to read the Base Stats of the Pokemon from the XML file, to give a template, this would be one of the Pokemon:
<Pokemon>
<Name>Bulbasaur</Name>
<BaseStats>
<Health>5</Health>
<Attack>5</Attack>
<Defense>5</Defense>
<SpecialAttack>7</SpecialAttack>
<SpecialDefense>7</SpecialDefense>
<Speed>5</Speed>
</BaseStats>
</Pokemon>
The code ive tried to use is:
XDocument pokemonDoc = XDocument.Load(#"File Path Here");
while(pokemonDoc.Descendants("Pokemon").Elements("Name").ToString() == cbSpecies.SelectedText)
{
var Stats = pokemonDoc.Descendants("Pokemon").Elements("BaseStats");
}
but this just returns pokemonDoc as null, any idea where im going wrong?
NOTE:
cbSpeciesSelect is where the user selects which species of pokemon they want.
The File Path definitely works as i've used it already in my program
The while loop never actually starts
Try xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var pokemon = doc.Descendants("Pokemon").Select(x => new {
name = (string)x.Element("Name"),
health = (int)x.Element("BaseStats").Element("Health"),
attack = (int)x.Element("BaseStats").Element("Attack"),
defense = (int)x.Element("BaseStats").Element("Defense"),
specialAttack = (int)x.Element("BaseStats").Element("SpecialAttack"),
specialDefense = (int)x.Element("BaseStats").Element("SpecialDefense"),
speed = (int)x.Element("BaseStats").Element("Speed"),
}).FirstOrDefault();
}
}
}
Can you try below code:
foreach(var e in pokemonDoc.Descendants("Pokemon").Elements("Name"))
{
if(e.Value==cbSpecies.SelectedText)
{
var Stats = pokemonDoc.Descendants("Pokemon").Elements("BaseStats");
}
}

How to pass a filename/path to text parser

I'm trying to get a simple text parser class to work in VS2015. I received the class code and built a basic Console Application, added the class Cawk and tried to compile/run it.
The main error that I get is
Argument 1: cannot convert from 'string' to 'System.IO.StreamReader'
It's clear that I can't figure out how to pass a filename through Main to Cawk. How do I give it an argument of a filename?
Any help or pointers would be appreciated.
My Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
string input = #"c:\temp\test.txt";
Cawk.Execute(input);
}
}
}
Snippet of My Cawk.cs:
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApplication3
{
public static class Cawk
{
public static IEnumerable<Dictionary<string, object>> Execute(StreamReader input)
{
Dictionary<string, object> row = new Dictionary<string, object>();
string line;
//string[] lines = File.ReadAllLines(path);
//read all rows
while ((line = input.ReadLine()) != null)
{
Execute accepts a StreamReader not a string.
Cawk.Execute(new StreamReader(#"c:\temp\test.txt"))
However, you should close the stream after you are done with it.
using (var sr = new StreamReader(#"c:\temp\test.txt"))
{
Cawk.Execute(sr);
}
something like:
var sr = new System.IO.StreamReader(#"c:\temp\test.txt");
Cawk.Execute(sr);
Simply use the File class from the System.IO namespace.
Cawk.Execute(File.OpenText(#"c:\temp\test.txt"));
Like this:
string input = #"c:\temp\test.txt";
Cawk.Execute(new System.IO.StreamReader(input));
You can put using System.IO; to the top like the rest of the usings, then you don't have to write it out later.

COMException when converting OneNote into HTML (but it works for PDF)

I am trying to convert a OneNote section into HTML and keep running into a persistent error. It's weird because the same code works if I convert to PDF. I'd appreciate help wit this one.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using OneNote = Microsoft.Office.Interop.OneNote;
using Office = Microsoft.Office.Core;
namespace trial
{
class Class1
{
[STAThread]
static int Main(string[] args)
{
String strXML;
OneNote.Application onApplication = new OneNote.Application();
onApplication.GetHierarchy(null,OneNote.HierarchyScope.hsPages, out strXML);
XDocument doc = XDocument.Parse(strXML);
XNamespace ns = doc.Root.Name.Namespace;
foreach (var sectionNode in from node in doc.Descendants(ns+"Section") select node)
{
string id = sectionNode.Attribute("ID").Value;
string path = "C:\\elixir.html";
if (id == "some specific ID")
{
//confirmed that it reaches this point
try
{
onApplication.Publish(id, path, OneNote.PublishFormat.pfHTML, ""); //PDF conversion Works! But HTML doesn't.
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
return 0;
}
}
}
Here are my references. All "Embed Interop Types" settings have been set to False.
Here is the exception message
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Office.Interop.OneNote.dll
Exception from HRESULT: 0x8004201A
I checked that this error is a "file already exists" error, but that's wrong. The destination file is definitely not present as program commences running.
Changing the extension from
string path = "C:\\elixir.html";
to
string path = "C:\\elixir.htm";
fixed it.

How to upload a document to SharePoint using WebServices

I have looked through multiple tutorials and asked many questions with NO result . Here it is step by step :
1) I use Microsoft Visual Studio 2012 . I do NOT have SP server installed and cannot use Microsoft.SharePoint.dll . However , I can use Web Service
2) I create a console project and add a WebReference like this
3) Here is my FULL code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication6;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
string srcUrl = #"C:\\xxx\\test.txt";
System.IO.FileStream fStream = System.IO.File.OpenRead(srcUrl);
string fileName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
ServiceWebReference.Lists listService = new ServiceWebReference.Lists(); // "Lists" get underlined with a red line ?
listService.ClientCredentials = System.Net.CredentialCache.DefaultCredentials;
try
{
// adding attachment
string result = listService.AddAttachment("testList", "1", fileName, contents);
Console.WriteLine(result);
}
catch (System.Web.Services.Protocols.SoapException e)
{
Console.WriteLine(e.GetBaseException());
Console.WriteLine(e);
}
}
}
}
Am I adding the reference in a right way ? If yes , then why does the ServiceWebReference.Lists listService ... ... ... get underlined in red (does not recognize namespace ) ?
How to make the code work ?
Since Lists.asmx is a SOAP Web service, it could be referenced as described in article: How to: Add a Reference to a Web Service
How to add Lists.asmx SOAP Web service in Visual Studio:
In Solution Explorer, right-click the name of the project that you want to add the service to, and then click Add Service Reference
The Add Service Reference dialog box appears, in the Service Reference Settings dialog box, click Add Web Reference as demonstrated below
Specify service endpoint Url
Visual Studio
Sample code
namespace SPUpload
{
class Program
{
static void Main(string[] args)
{
var attachmentUrl = UploadAttachment(#"c:\temp\usermanual.rtf","Phones","1");
}
private static string UploadAttachment(string filePath, string listName, string listItemId)
{
var listsSvc = new ListsService.Lists();
listsSvc.Credentials = System.Net.CredentialCache.DefaultCredentials;
var fileName = Path.GetFileName(filePath);
var fileContent = File.ReadAllBytes(filePath);
return listsSvc.AddAttachment(listName, listItemId, fileName, fileContent);
}
}
}

Problems with MessageBox.Show()

I'm new to code and most things work, but I can't get this code to run. Can someone help?
I tried using System.Forms but it showed as missing a namespace. When I used using System.Windows.Forms that message went away. It does not let me use both.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(#"file.csv");
// for set encoding
// StreamReader sr = new StreamReader(#"file.csv", Encoding.GetEncoding(1250));
string strline = "";
String[] _values = null;
int x = 0;
while(!sr.EndOfStream)
{
strline = sr.ReadLine();
_values = strline.Split(',');
if (_values.Length >= 6 && _values[0].Trim().Length > 0)
{
MessageBox.show(_values[1]);
}
}
sr.Close();
}
}
}
There is no such namespace System.Forms, the class you were trying to use (MessageBox) is in System.Windows.Forms. By correcting your using statement, the error went away.
Remember, you must have a reference to System.Windows.Forms.dll in your console app to use this class.
You need to reference System.Windows.Forms.dll in your project. Here is a detailed instruction how to do that.
There is no such namespace as System.Forms there is only a namespace called System.Windows.Forms, wich has the MessageBox class you are talking about. To be able to use it, you need to add a reference to the System.Windows.Forms.dll to to your project (find it in the .NET Tab in the "Add Reference ..." dialog) and it will work. Also note that MessageBox.Show() requires a capital 'S'. Please see below an optimized and fully working version of your code.
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
using (StreamReader sr = new StreamReader(#"file.csv"))
{
while (sr.Peek() >= 0)
{
string strline = sr.ReadLine();
string[] values = strline.Split(',');
if (values.Length >= 6 && values[0].Trim().Length > 0)
{
MessageBox.Show(values[1]);
}
}
}
}
}
}
You try use it in Console application first you should add System.Windows.Forms dll in your references (from .Net reference tab) then use it by adding it's namespace.
I'm a bit confused here. there is no namespace called System.Forms. It's always System.Windows.Forms. And the MessageBox class is defined in System.Windows.Forms
You need to manually ADD a reference to your project for System.Windows.Forms as you are on a console application and not a Windows Application. Just add the reference.

Categories

Resources