关于xml文档与ADO.net数据的转换问题
这是我在资料中看的代码,自己做了一些修改,所有代码都贴出来了,希望大侠给做个测试,看看问题到底处在哪。谢谢两个问题:一:每次选择listView1中的元素时都是出现Not...
这是我在资料中看的代码,自己做了一些修改,所有代码都贴出来了,希望大侠给做个测试,看看问题到底处在哪。谢谢
两个问题:
一:每次选择listView1中的元素时都是出现Not found提示,为什么?
二:第一次点击listView1中的元素时只出现一次提示,再点击另一个时会出现两次提示,即引发了两次Example_SelectedindexChanged事件,为什么?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
namespace xml_ADO.net
{
public partial class Form1 : Form
{
private XmlDocument doc = new XmlDocument();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listView1.SelectedIndexChanged += new EventHandler(Example_SelectedindexChanged );
DataSet ds = new DataSet("XMLAuthors");
SqlConnection conn = new SqlConnection("server=(local); integrated security=SSPI;database=Northwind");
SqlDataAdapter da = new SqlDataAdapter("select * from categories ",conn );
MemoryStream memstrm = new MemoryStream();
StreamReader strmRead = new StreamReader(memstrm );
StreamWriter strmWrite = new StreamWriter(memstrm );
da.Fill(ds,"Authors");
dataGridView1.DataSource = ds.Tables[0];
ds.WriteXml(strmWrite ,XmlWriteMode.IgnoreSchema );
memstrm.Seek(0, SeekOrigin.Begin);
doc.Load(strmRead);
doc.Save("mm.xml");
XmlNodeList nodelst = doc.SelectNodes("//CategoryName");
foreach (XmlNode nd in nodelst)
listView1.Items.Add(nd.InnerText );
}
private void Example_SelectedindexChanged(object sender,EventArgs e)
{
string srch = "/XMLAuthors/Authors[CategoryName= " + "'+ listView1.SelectedItems.ToString() + '" + "]";
XmlNode foundNode = doc.SelectSingleNode(srch );
if (foundNode != null)
MessageBox.Show(foundNode.OuterXml);
else
MessageBox.Show("Not found");
}
}
} 展开
两个问题:
一:每次选择listView1中的元素时都是出现Not found提示,为什么?
二:第一次点击listView1中的元素时只出现一次提示,再点击另一个时会出现两次提示,即引发了两次Example_SelectedindexChanged事件,为什么?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
namespace xml_ADO.net
{
public partial class Form1 : Form
{
private XmlDocument doc = new XmlDocument();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listView1.SelectedIndexChanged += new EventHandler(Example_SelectedindexChanged );
DataSet ds = new DataSet("XMLAuthors");
SqlConnection conn = new SqlConnection("server=(local); integrated security=SSPI;database=Northwind");
SqlDataAdapter da = new SqlDataAdapter("select * from categories ",conn );
MemoryStream memstrm = new MemoryStream();
StreamReader strmRead = new StreamReader(memstrm );
StreamWriter strmWrite = new StreamWriter(memstrm );
da.Fill(ds,"Authors");
dataGridView1.DataSource = ds.Tables[0];
ds.WriteXml(strmWrite ,XmlWriteMode.IgnoreSchema );
memstrm.Seek(0, SeekOrigin.Begin);
doc.Load(strmRead);
doc.Save("mm.xml");
XmlNodeList nodelst = doc.SelectNodes("//CategoryName");
foreach (XmlNode nd in nodelst)
listView1.Items.Add(nd.InnerText );
}
private void Example_SelectedindexChanged(object sender,EventArgs e)
{
string srch = "/XMLAuthors/Authors[CategoryName= " + "'+ listView1.SelectedItems.ToString() + '" + "]";
XmlNode foundNode = doc.SelectSingleNode(srch );
if (foundNode != null)
MessageBox.Show(foundNode.OuterXml);
else
MessageBox.Show("Not found");
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询