Got all mixed up and I'm sure it's a silly one.
Solution:
Project 1. Compania.
Linea.cs: Just the Linea class with different constructors and that's it for now.
Project 2. Bandeja.
Class.cs: Here I wrote all the methods I'll be needing when working with Linea. (getLinea() is the one I'll be showing you in the example below)
Project 3. WCFWebService.
A WCF service calling the C# methods.
References.
from Bandeja to Compania.
from WCFWebService to Compania.
from WCFWebService to Bandeja.
The only one error I get while building comes from the service.
Service Class
namespace WCFWebService
{
[DataContract]
public class WSBandeja : IWSBandeja
{
public Compania.Linea getLinea()
{
Compania.Linea linea = new Compania.Linea();
return linea.
}
}
}
When I enter return.linea. I can't find the method getLinea() contained in class.cs inside Project Bandeja, just the parameters.
Any suggestion is most welcome since I'm new to C# and WebServices.
Thanks.
EDIT.
Compania Project - Linea.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Compania
{
public class Linea
{
public string ani { get; set; }
public int teleprom { get; set; }
public string actividad { get; set; }
public DateTime fechaIngreso { get; set; }
public string reclamo { get; set; }
public string producto { get; set; }
public string observacion { get; set; }
public int tipoActividad { get; set; }
public string tipoAveria { get; set; }
public int reiteros { get; set; }
public int call { get; set; }
public bool trabajado { get; set; }
}
}
Bandeja Project - Class.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web;
namespace Bandeja
{
public class Bandeja
{
public static string getNewConnection()
{
return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;
}
public Compania.Linea getLinea()
{
var cLinea = new Compania.Linea();
string connectionString = getNewConnection();
SqlConnection conn = new SqlConnection(connectionString);
using(conn)
{
string variable = "GESTIONAR MANUALMENTE";
var command = new SqlCommand("Bandeja_test");
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("#linea", variable));
conn.Open();
SqlDataReader newReader = command.ExecuteReader();
while (newReader.Read())
{
cLinea = new Compania.Linea();
cLinea.ani = newReader["Línea"].ToString();
cLinea.fechaIngreso = Convert.ToDateTime(newReader["Fecha Ingreso"]);
cLinea.producto = newReader["Producto"].ToString();
cLinea.observacion = newReader["Observación"].ToString();
}
}
return cLinea;
}
}
}
The Web Service Interface.
namespace WCFWebService
{
[ServiceContract]
public interface IWSBandeja
{
[OperationContract]
Compania.Linea getLinea();
}
}
Looks like you are instantiating the wrong class. Try this.
[DataContract]
public class WSBandeja : IWSBandeja
{
public Compania.Linea getLinea()
{
Bandeja.Bandeja bandeja = new Bandeja.Bandeja();
return bandeja.getLinea();
}
}
Try
[ServiceContract]
public class WSBandeja : IWSBandeja
{
[OperationContract]
public Compania.Linea getLinea()
{
Compania.Linea linea = new Compania.Linea();
return linea.
}
}
And then define a [DataContract] for the complex type
namespace Compania
{
[DataContract]
public class Linea
{
[DataMember]
//whatever properties you have
}
See this page for more info on DataContracts and complex types
Related
I don't know how to inheritance a variables from another class. I write code in C# and I created two classes
First one is Osoba (engl. Person) which has variables ime, prezime, OIB (engl. name, last name, ID) and I have another class Racun (engl. account) which means bank account.
Class Racun has variables podaci o vlasniku računa (engl. account holder information), broj računa (engl. serial number of account) and stanje računa (engl. bank account balance).
Well podaci o vlasniku računa (engl. account holder information) needs to have variables from class Osoba. How can I do that?
I will show you my two created classes with code. If you notice both classes need to have 3 variables, I didn't create first variable in class Racun (engl. account) because the first one need to contain variables from class Osoba (engl. Person).
Osoba.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vjezba6_1
{
class Osoba
{
public string ime { get; set; }
public string prezime { get; set; }
public int oib { get; set; }
public Osoba(string tempIme, string tempPrezime, int tempOib)
{
this.ime = tempIme;
this.prezime = tempPrezime;
this.oib = tempOib;
}
}
}
Racun.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vjezba6_1
{
class Racun
{
public int brojRacuna { get; set; }
public int stanjeRacuna { get; set; }
public Racun(int tempPovr, int tempbrojRacuna, int tempstanjeRacuna)
{
this.povr = tempPovr;
this.brojRacuna = tempbrojRacuna;
this.stanjeRacuna = tempstanjeRacuna;
}
}
}
If your povr variable needs to hold the same pieces of information as in Osoba, you can either have povr be a reference to an instance of Osoba:
class Racun
{
public Osoba povr { get; set; }
public int brojRacuna { get; set; }
public int stanjeRacuna { get; set; }
public Racun(Osoba tempPovr, int tempbrojRacuna, int tempstanjeRacuna)
{
this.povr = tempPovr;
//etc
Or you could make a struct to hold common information:
namespace Vjezba6_1
{
struct PodaciOVlasnikuRacuna //i'm sure you can shorten this, but i don't know the language
{
public string ime;
public string prezime;
//other account holder information
}
}
And use this in your classes, like so:
namespace Vjezba6_1
{
class Osoba
{
public PodaciOVlasnikuRacuna podaci { get; set; }
public Osoba(string tempIme, string tempPrezime, int tempOib)
{
this.podaci.ime = tempIme;
this.podaci.prezime = tempPrezime;
this.podaci.oib = tempOib;
}
}
}
namespace Vjezba6_1_v2
{
class Osoba
{
public Podaci povr { get; set; }
public Osoba(string tempIme, string tempPrezime, int tempOib)
{
this.povr.ime = tempIme;
this.povr.prezime = tempPrezime;
this.povr.oib = tempOib;
}
}
}
Sorry for the somewhat basic question, but what can I say. I can't figure it out. The problem is that there's a foreach loop that's supposed to iterate through the rows (sections) and while it works for the first section, the second time through the loop it doesn't seem to read the second section. The same data is stored in version. BTW, the way the method is called I would be passing in ProductName as a parameter (There will be multiple products represented here and also a version number (e.g. v2.0.0) that I'll need to filter the results for too.
So I have an XML file that looks like this:
<Products>
<ProductName1>
<v2.0.0>
<GUID>"{B5ECEC43-5406-4E4D-96D9-456823100313}"</GUID>
<VersionNameToUninstall>"2.0.0 - 2.0.2"</VersionNameToUninstall>
<UninstallResponseFile>"GVQC-Client-2.0.0-Uninst.iss"</UninstallResponseFile>
</v2.0.0>
<v2.0.3>
<GUID>"{1D6C02D7-8E87-43BE-8AB2-1FF0E5ACD410}"</GUID>
<VersionNameToUninstall>"2.0.3"</VersionNameToUninstall>
<UninstallResponseFile>"GVQC-Client-2.0.3-Uninst.iss"</UninstallResponseFile>
</v2.0.3>
</ProductName1>
<ProductName2>
<v3.0.0>
<GUID>"{ABCDEC43-5406-4E4D-96D9-456823101234}"</GUID>
<VersionNameToUninstall>"2.2.0 - 2.2.2"</VersionNameToUninstall>
<UninstallResponseFile>"GVQC-Client-2.2.0-Uninst.iss"</UninstallResponseFile>
</v3.0.0>
<v4.0.0>
<GUID>"{5D6C02D7-8E87-43BE-8AB2-1FF0E5ACD589}"</GUID>
<VersionNameToUninstall>"4.0.0"</VersionNameToUninstall>
<UninstallResponseFile>"GVQC-Client-4.0.0-Uninst.iss"</UninstallResponseFile>
</v4.0.0>
</ProductName2>
</Products>
There will only be 10 or so versions (e.g. v2.x.x) so there's not a lot of data here. So I created a multidimensional (nested) class/struct to hold the data and when I try my code to read the data it's not working.
Here are the classes/stucts (I've tried both and neither works) that I'm trying to populate:
public class TopLevelObject
{
public string Version { get; set; }
public RowLevelObject Row {get;set;}
}
public struct RowLevelObject
{
public string Guid { get; set; }
public string VersionName { get; set; }
public string UninstallFileName { get; set; }
}
So here's my code. Please just ignore the Stream - that's so I can embed this XML file in the .exe and not have it be a separate file:
public static List<TopLevelObject> GetGUIDSFromFile(string GUIDKey)
List<InstallScriptMSIXMLTopLevelObject> installScriptMSIXMLTopLevelObjectList = new List<InstallScriptMSIXMLTopLevelObject>();
Stream GUIDXmlFileStream = typeof(PGCommonCA).Assembly.GetManifestResourceStream("PGCommonCA.ProductGUIDs.xml");
XElement xElement = XElement.Load(GUIDXmlFileStream);
var versions = xElement.Elements(GUIDKey).Descendants();
foreach (var version in versions)
{
TopLevelObject topLevelObject = new TopLevelObject();
RowLevelObject rowLevelObject = new RowLevelObject();
TopLevelObject.Version = version.Name.LocalName;
RowLevelObject.Guid = version.Element("GUID").Value;
RowLevelObject.VersionName = version.Element("VersionNameToUninstall").Value;
RowLevelObject.UninstallFileName = version.Element("UninstallResponseFile").Value;
TopLevelObjectList.Add(topLevelObject);
}
return TopLevelObjectList;
}
I know there are many ways to read XML and my choice doesn't work so I'm looking for another simple solution.
The following works :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement productName = doc.Root;
List<TopLevelObject> top = productName.Elements().Select(x => new TopLevelObject() {
Version = x.Name.LocalName,
Row = new RowLevelObject() {
Guid = (string)x.Element("GUID"),
VersionName = (string)x.Element("VersionNameToUninstall"),
UninstallFileName = (string)x.Element("UninstallResponseFile")
}
}).ToList();
}
}
public class TopLevelObject
{
public string Version { get; set; }
public RowLevelObject Row { get; set; }
}
public struct RowLevelObject
{
public string Guid { get; set; }
public string VersionName { get; set; }
public string UninstallFileName { get; set; }
}
}
I figured it out (many thanks to jdweng!!). Here's the final solution based on the revised XML at the top:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static TopLevelObject GetInfo(string xmlKey)
{
XDocument doc = XDocument.Load(FILENAME);
XElement productName = doc.Root;
List<TopLevelObject> top = productName.Descendants(xmlKey).Elements().Select(x => new TopLevelObject() {
Version = x.Name.LocalName,
Row = new RowLevelObject() {
Guid = (string)x.Element("GUID"),
VersionName = (string)x.Element("VersionNameToUninstall"),
UninstallFileName = (string)x.Element("UninstallResponseFile")
}
}).ToList();
}
}
public class TopLevelObject
{
public string Version { get; set; }
public RowLevelObject Row { get; set; }
}
public struct RowLevelObject
{
public string Guid { get; set; }
public string VersionName { get; set; }
public string UninstallFileName { get; set; }
}
}
My question is simple. I want to learn restsharp because of that. I used fake restful service "https://jsonplaceholder.typicode.com/posts" but queryResult is null what is wrong with it? how can ı get json data from "https://jsonplaceholder.typicode.com/posts" by using restsharp?
using RestSharp;
using System;
using System.Collections.Generic;
namespace ConsoleApp1.RestfulWebServ
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient("https://jsonplaceholder.typicode.com/");
var request = new RestRequest("posts/", Method.GET);
var queryResult = client.Execute<List<Person>>(request).Data;
}
}
internal class Person
{
public int userId { get; set; }
public int id { get; set; }
public string title { get; set; }
public string body { get; set; }
}
}
I have a list in C# with 4 items. This list is used to send a response in a web service and I need a specific order for the items, but I'm having a problem because, for some reason, the list changes the order when I fill it.
First, this is the class of the list
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace Mvm.SATWeb.Domain
{
[Serializable, DataContract]
public class puntoDeAtencion
{
public puntoDeAtencion()
{
}
[DataMember]
public string codigoPuntoAtencion { get; set; }
[DataMember]
public decimal montoIngreso { get; set; }
[DataMember]
public decimal montoEgreso { get; set; }
[DataMember]
public decimal ingresoNeto { get; set; }
}
}
I use a SQL server query to fill the list with a dataset
List<puntoDeAtencion> valores = new List<puntoDeAtencion>();
DataSet ds;
Database baseDatos = DatabaseFactory.CreateDatabase();
DbCommand comandoConsulta = baseDatos.GetStoredProcCommand("USP_RiesgoLiqui");
comandoConsulta.CommandTimeout = 600000;
baseDatos.AddInParameter(comandoConsulta, "#pvstrIdAgencia", DbType.String, "-1");
baseDatos.AddInParameter(comandoConsulta, "#pvstrFechaInicial", DbType.String, FechaIni);
baseDatos.AddInParameter(comandoConsulta, "#pvstrFechaFinal", DbType.String, FechaFin);
comandoConsulta.CommandTimeout = 1000000;
// baseDatos.ExecuteDataSet();
ds = baseDatos.ExecuteDataSet(comandoConsulta);
if (ds.Tables.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count ; i++)
{
// valores.Add(s)
//valores.Add (new punptoDeAtencion(){}
valores.Add(new puntoDeAtencion() { codigoPuntoAtencion = Convert.ToString(ds.Tables[0].Rows[i]["Agencia"]), montoIngreso = Convert.ToDecimal(ds.Tables[0].Rows[i]["INGRESONETO"]), montoEgreso = Convert.ToDecimal(ds.Tables[0].Rows[i]["MONTOEGRESO"]), ingresoNeto = Convert.ToDecimal(0.00) });
// var list1 = (from p in ds.Tables[0].Rows[i] select p).ToList();
}
}
return valores.ToList<puntoDeAtencion>();
This is the response (Using SOAP UI, but when I debug show the same values in the response object)
<b:listaPuntosDeAtencion>
<b:puntoDeAtencion>
<b:codigoPuntoAtencion>001</b:codigoPuntoAtencion>
<b:ingresoNeto>0</b:ingresoNeto>
<b:montoEgreso>53266155.0000</b:montoEgreso>
<b:montoIngreso>138285187.0000</b:montoIngreso>
</b:puntoDeAtencion>
and this is how it should be
<listaPuntosDeAtencion>
<puntoDeAtencion>
<codigoPuntoAtencion>00654</codigoPuntoAtencion>
<montoIngreso>79000.0</montoIngreso>
<montoEgreso>30000.0</montoEgreso>
<ingresoNeto>0.0</ingresoNeto>
</puntoDeAtencion>
I want to order the list or the response, I don't know if LINQ works in this case.
You can order them by using Order on DataMember like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace Mvm.SATWeb.Domain
{
[Serializable, DataContract]
public class puntoDeAtencion
{
public puntoDeAtencion()
{
}
[DataMember(Order = 0)]
public string codigoPuntoAtencion { get; set; }
[DataMember(Order = 1)]
public decimal montoIngreso { get; set; }
[DataMember(Order = 2)]
public decimal montoEgreso { get; set; }
[DataMember(Order = 3)]
public decimal ingresoNeto { get; set; }
}
}
Documentation here: https://msdn.microsoft.com/en-us/library/ms729813.aspx
Here's my issue : I need to get a list of resources from a web services, and deserialize it into object. But it doesn't work, despite the facts my code worked with another xml file. So I can't figure why it doesn't work, and I'm stuck with that !
Here's the XML :
<ResourceDataSet xmlns="http://schemas.microsoft.com/office/project/server/webservices/ResourceDataSet/">
<Resources>
<RES_UID>blabla</RES_UID>
<RES_NAME>blabla</RES_NAME>
<RES_CODE>blabla</RES_CODE>
<RES_GROUP>blabla</RES_GROUP>
<RES_COST_CENTER>blabla</RES_COST_CENTER>
</Resources>
<Resources>
<RES_UID>blabla</RES_UID>
<RES_NAME>blabla</RES_NAME>
<RES_CODE>blabla</RES_CODE>
<RES_GROUP>blabla</RES_GROUP>
<RES_COST_CENTER>blabla</RES_COST_CENTER>
</Resources>
<Resources>
<RES_UID>blabla</RES_UID>
<RES_NAME>blabla</RES_NAME>
<RES_CODE>blabla</RES_CODE>
<RES_GROUP>blabla</RES_GROUP>
<RES_COST_CENTER>blabla</RES_COST_CENTER>
</Resources>
</ResourceDataSet>
The class I want to deserialize into :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Threading.Tasks;
using System.Collections;
namespace TestWPF
{
[Serializable()]
public class Employee
{
[System.Xml.Serialization.XmlElement("RES_UID")]
public int RES_UID { get; set; }
[System.Xml.Serialization.XmlElement("RES_NAME")]
public String RES_NAME { get; set; }
[System.Xml.Serialization.XmlElement("RES_CODE")]
public String RES_CODE { get; set; }
[System.Xml.Serialization.XmlElement("RES_GROUP")]
public String RES_GROUP { get; set; }
[System.Xml.Serialization.XmlElement("RES_COST_CENTER")]
public String RES_COST_CENTER { get; set; }
public Employee()
{ }
public Employee(int r_id, String res_name, String res_code, String res_group, String res_cost_center)
{
this.RES_UID = r_id;
this.RES_NAME = res_name;
this.RES_CODE = res_code;
this.RES_GROUP = res_group;
this.RES_COST_CENTER = res_cost_center;
}
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("ResourceDataSet")]
public class EmployeeList //: IEnumerator, IEnumerable
{
public EmployeeList() {Items = new List<Employee>();}
[XmlArray("ResourceDataSet")]
[XmlArrayItem("Resources")]
public List<Employee> Items {get;set;}
}
}
And the code I use to deserialize :
EmployeeList lstEmployee = null;
XmlSerializer xs = new XmlSerializer(typeof(ServersList));
StreamReader sr = new StreamReader("testEmployee.xml");
lstEmployee = (EmployeeList)serializer.Deserialize(sr);
reader.Close();
for (int i = 0; i < lstEmployee.Items.Count(); i++)
{
MessageBox.Show(lstEmployee.Items[i].RES_NAME);
}
And when I try to launch I receive this error message :
Firstly your xml file is invalid - RES_UID is expecting an int, so even when you get your serialization working you'll run into that problem.
You're also not taking into account the namespace. The following class works:
[Serializable()]
public class Employee
{
[System.Xml.Serialization.XmlElement("RES_UID")]
public int RES_UID { get; set; }
[System.Xml.Serialization.XmlElement("RES_NAME")]
public String RES_NAME { get; set; }
[System.Xml.Serialization.XmlElement("RES_CODE")]
public String RES_CODE { get; set; }
[System.Xml.Serialization.XmlElement("RES_GROUP")]
public String RES_GROUP { get; set; }
[System.Xml.Serialization.XmlElement("RES_COST_CENTER")]
public String RES_COST_CENTER { get; set; }
public Employee()
{ }
public Employee(int r_id, String res_name, String res_code, String res_group, String res_cost_center)
{
this.RES_UID = r_id;
this.RES_NAME = res_name;
this.RES_CODE = res_code;
this.RES_GROUP = res_group;
this.RES_COST_CENTER = res_cost_center;
}
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("ResourceDataSet", Namespace = "http://schemas.microsoft.com/office/project/server/webservices/ResourceDataSet/")]
public class EmployeeList //: IEnumerator, IEnumerable
{
public EmployeeList() {Items = new List<Employee>();}
[XmlElement("Resources", Type = typeof(Employee))]
public List<Employee> Items {get;set;}
}
}
and your calling code with the typos fixed:
EmployeeList lstEmployee = null;
XmlSerializer xs = new XmlSerializer(typeof(EmployeeList));
StreamReader sr = new StreamReader("testEmployee.xml");
lstEmployee = (EmployeeList)xs.Deserialize(sr);
sr.Close();
for (int i = 0; i < lstEmployee.Items.Count(); i++)
{
MessageBox.Show(lstEmployee.Items[i].RES_NAME);
}
Remember to fix your xml to be ints otherwise it still won't work
You need to either decorate your root entity with the XmlRoot attribute or Or specify the root attribute when de serializing at runtime.
Here is a thread about this issue
https://stackoverflow.com/a/1557145/1305119