如何将xml值读取到数据集中 [英] How to read the xml values into dataset

查看:73
本文介绍了如何将xml值读取到数据集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,

我正在开发用于将xml文件读入数据集的项目.但是我遇到了错误.下面我已将我的编码发送给该应用程序.plz对其进行澄清. //authors.xml//

Hai,

I am developing the project for reading xml files into dataset.But i got the error.Below i has send my coding for this application.plz clarify it.
//authors.xml//

<authors_table>
  <authors>
    <au_id>172-32-1176</au_id>
    <au_lname>White</au_lname>
    <au_fname>Johnson</au_fname>
    <phone>408 496-7223</phone>
    <address>10932 Bigge Rd.</address>
    <city>Menlo Park</city>
    <state>CA</state>
    <zip>94025</zip>
    <contract>true</contract>
  </authors>
  <authors>
    <au_id>213-46-8915</au_id>
    <au_lname>Green</au_lname>
    <au_fname>Margie</au_fname>
    <phone>415 986-7020</phone>
    <address>309 63rd St. #411</address>
    <city>Oakland</city>
    <state>CA</state>
    <zip>94618</zip>
    <contract>true</contract>
  </authors>
  <authors>
    <au_id>238-95-7766</au_id>
    <au_lname>Carson</au_lname>
    <au_fname>Cheryl</au_fname>
    <phone>415 548-7723</phone>
    <address>589 Darwin Ln.</address>
    <city>Berkeley</city>
    <state>CA</state>
    <zip>94705</zip>
    <contract>true</contract>
  </authors>
  <authors>
    <au_id>267-41-2394</au_id>
    <au_lname>Hunter</au_lname>
    <au_fname>Anne</au_fname>
    <phone>408 286-2428</phone>
    <address>22 Cleveland Av. #14</address>
    <city>San Jose</city>
    <state>CA</state>
    <zip>95128</zip>
    <contract>true</contract>
  </authors>
  <authors>
    <au_id>274-80-9391</au_id>
    <au_lname>Straight</au_lname>
    <au_fname>Dean</au_fname>
    <phone>415 834-2919</phone>
    <address>5420 College Av.</address>
    <city>Oakland</city>
    <state>CA</state>
    <zip>94609</zip>
    <contract>true</contract>
  </authors>
</authors_table>




然后我在表单中添加1个datagrid,1textbox和2button控件
//后面的代码//




Then i add 1 datagrid, 1textbox and 2button controls in my form
//code behind//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication100
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("authors");
            
        }

        private void btnReadXML_Click(object sender, EventArgs e)
        {
            string filePath = "C:/Documents and Settings/sridharan/My Documents/Visual Studio 2008/Projects/WindowsFormsApplication100/authors.xml";
            ds.ReadXml(filePath);
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "authors";
            dataGridView1.CaptionText = dataGridView1.DataMember;
            
        }

        private void BtnShowSchema_Click(object sender, EventArgs e)
        {
            System.IO.StringWriter swXML = new System.IO.StringWriter();
            ds.WriteXmlSchema(swXML);
textBox1.Text = swXML.ToString();
        }
    }
}



在后面的代码中有一行

"dataGridView1.CaptionText = dataGridView1.DataMember;"
错误显示如下
///错误///
错误3" System.Windows.Forms.DataGridView"不包含"CaptionText"的定义,并且没有扩展方法"CaptionText"接受类型为"System.Windows.Forms.DataGridView"的第一个参数可以找到'(您是否缺少using指令或程序集引用?)C:\ Documents and Settings \ sridharan \ My Documents \ Visual Studio 2008 \ Projects \ WindowsFormsApplication100 \ WindowsFormsApplication100 \ Form1.cs 32 27 ReadingXML"

然后也出现错误名称ds在当前上下文中不存在".请清除我的疑问.



In the code behind there is a line

" dataGridView1.CaptionText = dataGridView1.DataMember;"
the error shows like this
///ERROR///
"Error 3 ''System.Windows.Forms.DataGridView'' does not contain a definition for ''CaptionText'' and no extension method ''CaptionText'' accepting a first argument of type ''System.Windows.Forms.DataGridView'' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\sridharan\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication100\WindowsFormsApplication100\Form1.cs 32 27 ReadingXML"

Then also the error "name ds doesnot exist in current context".Plz clear my doubt.

推荐答案

错误消息本身具有答案. DataGridview不具有CaptionText
的属性
如果希望该属性存在,则创建一个扩展datagridview类的自定义datagrid视图.
The error message itself has the answer. DataGridview don''t have the property of CaptionText

If you want that property to be there then then create a custom datagrid view extending the datagridview class.


Dataset对象ds已在Form_Load函数中声明,因此没有其他函数可以使用它.
因此,将数据库对象声明为字段.
像这样:

The Dataset object ds has been declared in the Form_Load function and hence no other function can use it.
So declare the Database object as a field.
Like this :

public partial class Form1 : Form
   {
       DataSet ds;
       public Form1()
       {
       }



现在,此ds将可用于此类的功能.
然后在form_load中,实例化它.



Now this ds will be available to this class''s functions.
And in the form_load, just instantiate it.

private void Form1_Load(object sender, EventArgs e)
        {
            ds = new DataSet("authors");
        }



关于CaptionText,那是因为您的DatagridView不具有CaptionText属性.



About the CaptionText, that''s because your DatagridView does not have the property CaptionText.


朋友们,

我也遇到了同样的错误,然后说如何创建扩展datagridview类的自定义datagridview以及如何设置Caption文本的属性


dataGridView1.CaptionText = dataGridView1.DataMember;
{
如果(dataGridView1.CaptionText ==")
dataGridView1.CaptionText ="authors DataGrid";
}
在上面的编码部分中,我得到了错误提示,因为(``System.Windows.Forms.DataGridView''不包含``CaptionText''的定义,也没有扩展方法``CaptionText''接受类型为''的第一个参数可以找到System.Windows.Forms.DataGridView''(您是否缺少using指令或程序集引用?)


请说出正确的解决方法.
Hi Frnds,

me too getting same error then say how to create a custom datagridview extending the datagridview class and how to set the property of Caption text


dataGridView1.CaptionText= dataGridView1.DataMember;
{
if (dataGridView1.CaptionText =="")
dataGridView1.CaptionText ="authors DataGrid";
}
in the above coding part i am getting error as (''System.Windows.Forms.DataGridView'' does not contain a definition for ''CaptionText'' and no extension method ''CaptionText'' accepting a first argument of type ''System.Windows.Forms.DataGridView'' could be found (are you missing a using directive or an assembly reference?)


Please say the proper solution Frnds.


这篇关于如何将xml值读取到数据集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆