从控制台应用程序通过通用列表从asp.net
我有一个解决方案,我需要从asp.net调用控制台应用程序,并需要传递变量。一个变量是某个类的通用列表。从控制台应用程序通过通用列表从asp.net
我试过通过它,但我得到错误,我无法将通用列表转换为正确的字符串。
我不知道是否有另一种方式来通过这个。
我知道webservice可以解决这个问题。但它还有其他选择吗?
这是可以做到的或仅是字符串可能通过
Here is the generic list sample.
List<person> personList = new List<person>();
person p = new person();
p.name = "test";
p.age = 12;
p.birthdate = 01/01/2014
personList.add(p)
感谢。
好的,控制台应用程序只接受字符串。这是在Main
方法定义为
static void Main(string[] args)
既然你有一个复杂的对象列表它会有点难以将此信息传递给控制台应用程序(但并非不可能)。有几种选择给你。
- 只要字符串不是太长,将您的值作为逗号分隔值传递给字符串。
-
Web Services
或Web API
正如您所建议的。 -
Serialize
将您的对象添加到XML文件中,然后在您的控制台应用程序中使用deserialize
。 - 写和持久性数据存储读取
选项3(写XML文件)
我出来写此示例代码更新
示例代码好奇心。希望这有助于解决您的问题。
ASP.Net网站
我有一个按钮在我的网页(Default.aspx
),并在它的单击事件它的人集合/列表写入到一个XML文件。这是后面的代码。
using System;
using System.IO;
using System.Xml.Serialization;
namespace WriteToConsole
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnWriteToConsole_Click(object sender, EventArgs e)
{
PersonCollection personList = new PersonCollection();
// Person 1
Person p = new Person();
p.Name = "test 1";
p.Age = 12;
p.BirthDate = DateTime.Parse("01/01/2014");
personList.Add(p);
// Person 2
Person p2 = new Person();
p2.Name = "test 2";
p2.Age = 25;
p2.BirthDate = DateTime.Parse("01/01/2014");
personList.Add(p2);
XmlSerializer serializer = new XmlSerializer(personList.GetType());
StreamWriter file = new StreamWriter(@"D:\temp\PersonCollection.xml");
serializer.Serialize(file, personList);
file.Close();
}
}
}
而且,Person.cs
看起来像这样。
using System;
using System.Collections.Generic;
namespace WriteToConsole
{
[Serializable]
[System.Xml.Serialization.XmlRoot("PersonCollection")]
public class PersonCollection : List<Person> {
}
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime BirthDate { get; set; }
public Person()
{
this.Name = string.Empty;
this.Age = 0;
this.BirthDate = DateTime.MinValue;
}
}
}
控制台应用程序
然后阅读您的控制台应用程序的XML文件,并在控制台上显示personList
数据。
using System;
using System.IO;
using System.Xml.Serialization;
namespace ReadInConsole
{
class Program
{
static void Main(string[] args)
{
XmlSerializer deserializer = new XmlSerializer(typeof(PersonCollection));
TextReader textReader = new StreamReader(@"D:\temp\PersonCollection.xml");
PersonCollection personList = new PersonCollection();
personList = (PersonCollection)deserializer.Deserialize(textReader);
textReader.Close();
if (personList != null && personList.Count > 0)
{
foreach (Person p in personList)
{
Console.WriteLine("Person name: {0}, Age: {1} and DOB: {2}", p.Name, p.Age, p.BirthDate.ToShortDateString());
}
Console.ReadLine();
}
}
}
}
在控制台应用程序,你应该有相同的Person类为模式(这是相同的Web应用程序的Person类,只有命名空间是不同的)。
using System;
using System.Collections.Generic;
namespace ReadInConsole
{
[Serializable]
[System.Xml.Serialization.XmlRoot("PersonCollection")]
public class PersonCollection : List<Person>
{
}
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime BirthDate { get; set; }
public Person()
{
this.Name = string.Empty;
this.Age = 0;
this.BirthDate = DateTime.MinValue;
}
}
}
希望你明白代码。
我曾尝试通过为XML。但是元素被标记为不同的参数。 可以说我通过一个人xml。
你能告诉我你是怎么做到的吗?然后我可以解决问题 – Sam 2014-10-20 03:58:12
XDocument doc = new XDocument(new XElement(“PersonInfo”,new XElement(“Persons”,personlist.Select(x => new XElement(“Person”,new XAttribute(“Name” ,x.Name)))))); – Mico 2014-10-20 05:12:47
什么在你的通用列表?显示一些代码示例 – Sam 2014-10-20 03:17:33
@sam我更新了问题。感谢您的期待 – Mico 2014-10-20 03:26:35