问题出在else的情况下,我已经对问题行进行了评论
static void Main(string[] args)
{
XMLData xmldataExample = new XMLData();
Usergaz usergazExample = new Usergaz();
UsergazItem usergazitemExample = new UsergazItem();
UsergazItem item = new UsergazItem();
string filename = @"C:\Users\565133\Desktop\test.xml";
Deserialize<XMLData> deserializeobj = new Deserialize<XMLData>();
Console.WriteLine("Choose \n1.Serialization\n2.Deserialization\n3.Deserialize only a token");
//It works when I give tknid input line here
int inp = Console.Read();
string tknid = Console.ReadLine();//It doesn't work if I get tknid input here
if (inp == 1)
{
usergazitemExample.getusergazitem();
usergazExample.getusergaz();
usergazExample.gazitem.Add(usergazitemExample);
xmldataExample.getxmldata();
xmldataExample.gazset.Add(usergazExample);
serial(xmldataExample, filename);
Console.WriteLine("Its done");
Console.ReadKey();
}
else if (inp == 2)
{
Console.WriteLine("Getting data from xml file");
// Deserialize<XMLData> deserializeobj = new Deserialize<XMLData>();
xmldataExample = deserializeobj.deserializefunction(filename);
List<Usergaz> samplelist = new List<Usergaz>();
samplelist = xmldataExample.gazset;
MTable.initialize();
MTable.usergazzetterset.Add(xmldataExample.updatedtime, samplelist);
Console.WriteLine("Deserialization complete, check the object");
Console.ReadKey();
}
在其他情况下,我使用tknid进行过滤,但即使在我从用户输入tknid之前,我也会得到null引用异常,说'item'对象为null。 我得到了指向Console.WriteLine Line的异常
else
{
//Console.WriteLine("Getting only a specific token Enter the token id");
//string tknid;
//tknid = Console.ReadLine();
//I intended to give tknid input here, but I cannot do it...
//Console.WriteLine(tknid);
//Console.ReadKey();
//string tknid = "3"; Initializing tknid with this statement works fine
item = deserializeobj.deserializefunction(filename).gazset[0].gazitem.Where(X => X.id == tknid).FirstOrDefault();
Console.WriteLine("The value for the token id {0} is {1}", tknid,item.val);
Console.ReadKey();
}
}
答案 0 :(得分:0)
我已经得到了解决方案,问题不在于if..else语句,它与Console.Read()有关。
我将SELECT posts.id , posts.cat_id , posts.school_id , posts.campus_id , posts.status , posts.priority , posts.title , posts.content , posts.phone , posts.email , posts.tags , posts.zip , posts.price_new , posts.price_old , posts.reviewed_by , posts.reviewed_date , posts.updated_date , posts.posted_date , posts.expired_date , posts.ip_address , schools.school_name, campuses.campus_name , meta.meta_key , meta_value , images.img_name
FROM posts LEFT JOIN
meta
ON posts.id = meta.post_id LEFT JOIN
images
ON posts.id = images.post_id LEFT JOIN
schools
ON posts.school_id = schools.id LEFT JOIN
campuses
ON posts.campus_id = campuses.id
ORDER BY posts.updated_date DESC LIMIT 10
替换为int inp = Console.Read()
,但效果很好。
现在,我了解int inp = int.Parse(Console.ReadLine())
如何在我在下面分享的答案的帮助下工作,但我永远不会尝试使用Console.Read()
,并始终使用Console.Read()
。
Console.ReadLine()
如何运作: