大家来看看啊,我做这个程序的主要目的是,把 XML文件读出来,并在另外一个程序的3个textbox中依次填写。 
所以我模拟键盘操作复制内容,但是运行之后在另外程序的Textbox中模拟粘贴时,为什么每次只能粘贴出最后一个数据(5.95)Tab是模拟textbox之间切换。如下XML文件 
<bookstore> 
<table> 
    <title>Oberon's Legacy </title> 
    <author>Corets, Eva </author> 
    <price>5.95 </price> 
</table> 
</bookstore> 
请告高手帮我改改程序。private void button1_Click(object sender, EventArgs e)
        {   //这个button作用从指定目录选择XML文件(这里没问题)            Stream myStream;            openFileDialog1.InitialDirectory = " ";            openFileDialog1.Filter = "txt files (*.xml)|*.xml";            openFileDialog1.RestoreDirectory = true;            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    myStream.Close();
                }
            }            this.textBox1.Text = this.openFileDialog1.FileName;        }
        private void button2_Click(object sender, EventArgs e)
        {               DataSet ds = new DataSet();            ds.ReadXml(this.textBox1.Text);            this.dataGridView1.DataSource = ds.Tables[0];            DataTable dataTable = new DataTable();
            //以上是将XML数据填充到DataGridView中            XmlDocument xd = new XmlDocument();            xd.Load(this.textBox1.Text);            foreach (XmlNode xn in xd.FirstChild.ChildNodes)
            {
                foreach (XmlNode xxn in xn.ChildNodes)
                {
                    Clipboard.SetText(xxn.InnerText);                    Thread.Sleep(500);
                    
                    SendKeys.SendWait("^V");                    SendKeys.SendWait("{TAB}");
                }
            }
            //主要是这里遍历XML文件后,我在另一个程序的TextBox中模拟粘贴时,每次粘贴出的内容都是XML文件的最后一个数据“5.95”.
        }请C#达人帮我改改,真的是救命程序啊。在做不出来我完完了。

解决方案 »

  1.   

                foreach (XmlNode xn in xd.FirstChild.ChildNodes)
                {
                    foreach (XmlNode xxn in xn.ChildNodes)
                    {
                        Clipboard.SetText(xxn.InnerText);                    Thread.Sleep(500);
                        
                        SendKeys.SendWait("^V");                    SendKeys.SendWait("{TAB}");
                    }
                }感觉是这里的问题,这里最后只取了5.59,不知道你其他代码 都没法测试
      

  2.   

    所有代码都在这里了,页面就2个 BUTTON,一个grridview,你把代码考了测试下就知道了
      

  3.   

    我照了你的代码写了份程序,好像没什么问题(全部代码贴出):using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;
    using System.Xml;
    using System.Threading;
    namespace test
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.OpenFileDialog openFileDialog1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.textBox4 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(48, 24);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(344, 21);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(48, 96);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(264, 21);
    this.textBox2.TabIndex = 1;
    this.textBox2.Text = "";
    // 
    // textBox3
    // 
    this.textBox3.Location = new System.Drawing.Point(48, 136);
    this.textBox3.Name = "textBox3";
    this.textBox3.Size = new System.Drawing.Size(264, 21);
    this.textBox3.TabIndex = 2;
    this.textBox3.Text = "";
    // 
    // textBox4
    // 
    this.textBox4.Location = new System.Drawing.Point(48, 168);
    this.textBox4.Name = "textBox4";
    this.textBox4.Size = new System.Drawing.Size(264, 21);
    this.textBox4.TabIndex = 3;
    this.textBox4.Text = "";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(432, 24);
    this.button1.Name = "button1";
    this.button1.TabIndex = 4;
    this.button1.Text = "上传";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(552, 24);
    this.button2.Name = "button2";
    this.button2.TabIndex = 5;
    this.button2.Text = "获取数据";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(744, 541);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox4);
    this.Controls.Add(this.textBox3);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    Stream myStream; openFileDialog1.InitialDirectory = " "; openFileDialog1.Filter = "txt files (*.xml)|*.xml"; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    if ((myStream = openFileDialog1.OpenFile()) != null)
    {
    myStream.Close();
    }
    } this.textBox1.Text = this.openFileDialog1.FileName; } private void button2_Click(object sender, System.EventArgs e)
    {
    DataSet ds = new DataSet(); ds.ReadXml(this.textBox1.Text); //this.dataGridView1.DataSource = ds.Tables[0]; DataTable dataTable = new DataTable();
    //以上是将XML数据填充到DataGridView中 XmlDocument xd = new XmlDocument(); xd.Load(this.textBox1.Text);

    textBox2.Focus(); foreach (XmlNode xn in xd.FirstChild.ChildNodes)
    {
    foreach (XmlNode xxn in xn.ChildNodes)
    {
    Clipboard.SetDataObject(xxn.InnerText); Thread.Sleep(500);
                        
    SendKeys.SendWait("^V"); SendKeys.SendWait("{TAB}");
    }
    }
    }
    }
    }
      

  4.   

    我想说明下,这个程序只是读取XML并模拟键盘操作,把数据放到剪切板。当程序运行后,要到TXT中测试下,是否粘贴第一个数据,在tab键...依次操作下去。把3个数据粘贴出来
      

  5.   

    呵呵   foreach (XmlNode xn in xd.FirstChild.ChildNodes)
                {
                    foreach (XmlNode xxn in xn.ChildNodes)
                    {
                        Clipboard.SetText(xxn.InnerText);                    Thread.Sleep(500);
                        
                        SendKeys.SendWait("^V");                    SendKeys.SendWait("{TAB}");
                    }
                }
                //主要是这里遍历XML文件后,我在另一个程序的TextBox中模拟粘贴时,每次粘贴出的内容都是XML文件的最后一个数据“5.95”.
            }
    在这个程序中:    
                        SendKeys.SendWait("^V");                    SendKeys.SendWait("{TAB}");
    之前你是否要定义下:文本框是否被选中:  因为你要粘贴到哪个里面先要选中这个文本框吧!
      

  6.   

    请热心并不厌其烦的高手看能否加我QQ,我把需求和程序给你们看看本人QQ是170506973
      

  7.   

    请那位厉害的版主,帮忙改下程序啊。我就是想在运行程序后,可以在文本里依次粘贴出XML文件。每次粘贴后按tab键,在进行下一个粘贴...请帮忙了,跪求大虾了。已经在MSDN里求了2个多礼拜了,还没得到解决。我快崩溃了。
      

  8.   

    问题原因我知道了,剪切板每次复制都会把前面的内容覆盖掉。所以它只能得到最后一条数据。所以模拟剪切板的操作好像不能完成我的需求。我想请大虾们,介绍下。当我把XML文件读出来以后,怎么可以把读出的文件内容在另外文本文件里依次显示出来。