如何根据检索到的数据创建多个列表?

发布于 2024-12-17 11:06:36 字数 4074 浏览 0 评论 0原文

我正在使用一个 Web 服务来获取数据,并且我成功地完全取回了数据,

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{ String username = txtblock4.Text.Trim();
String hash = txtblock8.Text.Trim();
client.UploadStringAsync(new    
Uri("http://www.picturelove.mobi/picturelove3/getmessages.php?loginType=N&email=" +   
username + "&hash=" + hash), "Post");
client.UploadStringCompleted += new   
UploadStringCompletedEventHandler(client_UploadStringCompleted);
}

我正在解析 xml 响应,如下所示,使用两个函数保存消息数据并生成消息数据,我正在将数据放入列表中。

void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{

if (e.Error != null)
txtblock10.Text = e.Error.Message.Trim();
else
txtblock10.Text = e.Result.Trim();
String XmlString = txtblock10.Text.Trim();
using (XmlReader reader = XmlReader.Create(new StringReader(XmlString)))
{

while (reader.ReadToFollowing("all_messages"))
{
while (reader.Read())
{
try
{
reader.ReadToFollowing("id");
string id = reader.ReadElementContentAsString();

reader.MoveToAttribute("from");
string n_from = reader.ReadElementContentAsString();

reader.MoveToAttribute("to");
string n_to = reader.ReadElementContentAsString();

reader.MoveToAttribute("time");
string n_time = reader.ReadElementContentAsString();

reader.MoveToAttribute("sub");
string n_sub = reader.ReadElementContentAsString();

reader.MoveToAttribute("ct");
string n_ct = reader.ReadElementContentAsString();

reader.MoveToAttribute("txt");
string n_txt = reader.ReadElementContentAsString();

reader.MoveToAttribute("msg_image");
string n_image = reader.ReadElementContentAsString();

reader.MoveToAttribute("gender");
string n_gender = reader.ReadElementContentAsString();

reader.MoveToAttribute("name");
string n_name = reader.ReadElementContentAsString();

reader.MoveToAttribute("avatar");
string n_avatar = reader.ReadElementContentAsString();
ObservableCollection<SampleData> dataSource = new ObservableCollection<SampleData>();
dataSource.Add(new SampleData() { Name = txtblock11.Text, Text = txtblock12.Text, 
Time= txtblock13.Text, Picture = txtblock9.Text });
// listBox.Items.Add(new SampleData() { Name = txtblock11.Text, Text = txtblock8.Text,                 
Time = txtblock5.Text, Picture = txtblock12.Text });
SaveMessageData(new SampleData() { Name = txtblock11.Text, Text = txtblock12.Text, Time     
= txtblock13.Text, Picture = txtblock9.Text });
// listBox1.ItemsSource =
this.GenerateMessageData();
}
catch
{
//MessageBox.Show("No New Messages For You", "No Message", MessageBoxButton.OK);
break;
}
}
}
}
}
}
public class SampleData
{
public string Name { get; set; }
public string Text { get; set; }
public string Time { get; set; }
public string Picture { get; set; }
}
public void SaveMessageData()
{
using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isoStream = new IsolatedStorageFileStream("MyTextfile.txt", FileMode.Append,             
isoStorage))
    {
XmlSerializer serializer = new XmlSerializer(typeof(List<SampleData>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
List<SampleData> data = new List<SampleData>();
foreach (SampleData obj in Listbox.Items)
{
data.Add(obj);


}

data.Add(msg);
if (data != null)
serializer.Serialize(xmlWriter, data);

}
}
}

}
}

public void GenerateMessageData()
{

List<SampleData> data;// = new List<SampleData>();
try
{
using (IsolatedStorageFile myIsolatedStorage =     
IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("MyTextfile.txt",     
 FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<SampleData>));
data = (List<SampleData>)serializer.Deserialize(stream);
this.Listbox.ItemsSource = data;
return data;
}
}
}
catch (Exception exp)
{
MessageBox.Show("No New Messages For You", "No Message", MessageBoxButton.OK);
}
return null;
}

但是,真正的问题是,如果有两组数据(两条消息),如果我得到的数据都显示在同一个列表中。如果有多个数据,如何操作或迭代多个列表?

I am consuming a webservice for getting the data and i am success fully getting back the data

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{ String username = txtblock4.Text.Trim();
String hash = txtblock8.Text.Trim();
client.UploadStringAsync(new    
Uri("http://www.picturelove.mobi/picturelove3/getmessages.php?loginType=N&email=" +   
username + "&hash=" + hash), "Post");
client.UploadStringCompleted += new   
UploadStringCompletedEventHandler(client_UploadStringCompleted);
}

I am parsing the xml response like below with two functions save message data and generate message data i am gettin the data in a list.

void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{

if (e.Error != null)
txtblock10.Text = e.Error.Message.Trim();
else
txtblock10.Text = e.Result.Trim();
String XmlString = txtblock10.Text.Trim();
using (XmlReader reader = XmlReader.Create(new StringReader(XmlString)))
{

while (reader.ReadToFollowing("all_messages"))
{
while (reader.Read())
{
try
{
reader.ReadToFollowing("id");
string id = reader.ReadElementContentAsString();

reader.MoveToAttribute("from");
string n_from = reader.ReadElementContentAsString();

reader.MoveToAttribute("to");
string n_to = reader.ReadElementContentAsString();

reader.MoveToAttribute("time");
string n_time = reader.ReadElementContentAsString();

reader.MoveToAttribute("sub");
string n_sub = reader.ReadElementContentAsString();

reader.MoveToAttribute("ct");
string n_ct = reader.ReadElementContentAsString();

reader.MoveToAttribute("txt");
string n_txt = reader.ReadElementContentAsString();

reader.MoveToAttribute("msg_image");
string n_image = reader.ReadElementContentAsString();

reader.MoveToAttribute("gender");
string n_gender = reader.ReadElementContentAsString();

reader.MoveToAttribute("name");
string n_name = reader.ReadElementContentAsString();

reader.MoveToAttribute("avatar");
string n_avatar = reader.ReadElementContentAsString();
ObservableCollection<SampleData> dataSource = new ObservableCollection<SampleData>();
dataSource.Add(new SampleData() { Name = txtblock11.Text, Text = txtblock12.Text, 
Time= txtblock13.Text, Picture = txtblock9.Text });
// listBox.Items.Add(new SampleData() { Name = txtblock11.Text, Text = txtblock8.Text,                 
Time = txtblock5.Text, Picture = txtblock12.Text });
SaveMessageData(new SampleData() { Name = txtblock11.Text, Text = txtblock12.Text, Time     
= txtblock13.Text, Picture = txtblock9.Text });
// listBox1.ItemsSource =
this.GenerateMessageData();
}
catch
{
//MessageBox.Show("No New Messages For You", "No Message", MessageBoxButton.OK);
break;
}
}
}
}
}
}
public class SampleData
{
public string Name { get; set; }
public string Text { get; set; }
public string Time { get; set; }
public string Picture { get; set; }
}
public void SaveMessageData()
{
using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isoStream = new IsolatedStorageFileStream("MyTextfile.txt", FileMode.Append,             
isoStorage))
    {
XmlSerializer serializer = new XmlSerializer(typeof(List<SampleData>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
List<SampleData> data = new List<SampleData>();
foreach (SampleData obj in Listbox.Items)
{
data.Add(obj);


}

data.Add(msg);
if (data != null)
serializer.Serialize(xmlWriter, data);

}
}
}

}
}

public void GenerateMessageData()
{

List<SampleData> data;// = new List<SampleData>();
try
{
using (IsolatedStorageFile myIsolatedStorage =     
IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("MyTextfile.txt",     
 FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<SampleData>));
data = (List<SampleData>)serializer.Deserialize(stream);
this.Listbox.ItemsSource = data;
return data;
}
}
}
catch (Exception exp)
{
MessageBox.Show("No New Messages For You", "No Message", MessageBoxButton.OK);
}
return null;
}

But, The real problem is if there are two set of data(two messages) if i'm getting both are showing in the same list. How to manipulate or iterate multiple lists if there are multiple data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-12-24 11:06:36

如果您需要合并两个列表,并且两个列表中可能都有项目,但您只想在合并列表中出现一次,最简单的解决方案是在添加之前简单地检查它是否已经在列表中。

If you need to merge two lists and there may be items which exist in both but you only want to appear in the merged list once, the easiest solution is to simply check if it's already in the list before adding it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文