无法读取DataSet的xml? [英] Can't read xml for DataSet?

查看:84
本文介绍了无法读取DataSet的xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我创建DataSet对象

 DataSet ds =  new  DataSet(); 



然后我加载xmlscheme。

 ds.ReadXmlSchema(  C:/ Users / Levan / Dcuments / Visual Studio 2012 / Projects / Crashop / CarLibrary / CarDataSet.xsd); 

它加载很好。我使用数据集可视化器进行调试.Xmlscheme具有以下结构表

汽车(carname(字符串),carnumber(字符串),cardiscription(字符串),CarID(int))。比我尝试loadxml

 ds.ReadXml(  C:/ Users / Levan / Documents / Visual Studio 2012 / Projects / Crashop / CarLibrary / CarPark.xml); 

xml文件具有以下结构

 <?  xml     version   =  1.0    standalone   =   >  
< NewDataSet >
< 汽车 >
< carname > BMW < / carname >
< carnumber > AA2935IO < / carnumber >
< cardiscription > 汽车< / cardiscription >
< ; CarID > 1 < / CarID >
< ; / Cars >
< 汽车 >
< carname > Mersedes < / carname >
< carnumber > AA2235IO < / carnumber >
< cardiscription > 好车< / cardiscription >
& CarID > 2 < / CarID >
< / Cars >
< / NewDataSet >



结果没有错误,但空表。

 ds.Tables [ 汽车]。行。添加(  BMW  AA2935IO 好车 1 ); 

工作正常。但我想知道如何从XML文件中读取。在我使用一切都很好的例子中,我找不到合适的解决方案。

解决方案

至少有几种读取/写入xml数据的方法,例如:

1. Linq to XML [ ^ ]

2. Xml序列化/反序列化 [ ^ ]



基于这个 [ ^ ]我建议创建c ustom类集合如下:



汽车类用于存储单车数据。

  public   class  Car 
{
private string sCarName = string .Empty;
private string sCarNumber = string .Empty;
private string sCarDescription = string .Empty;
private int iCarId = 0 ;

// contructor
public Car( string _CarName, string _CarNumber, string _CarDescription, int _CarId)
{
sCarName = _CarName;
sCarNumber = _CarNumber;
sCarDescription = _CarDescription;
iCarId = _CarId;
}

public string CarName
{
get { return sCarName;}
set {sCarName = value ;}
}

public string CarNumber
{
get { return sCarNumber;}
set {sCarNumber = value ;}
}

public string CarDescription
{
get { return sCarDescription;}
设置 {sCarDescription = value ;}
}

public int CarID
{
get { return iCarId ;}
set {iCarId = value ;}
}

}





汽车类继承自 CollectionBase 并用于存储汽车列表(类型:汽车):

  public   class 汽车:CollectionBase 
{

public Car Item( int index)
{
if (index< 0 || index> List.Count-1)
throw new IndexOutOfRangeException();

return (Car)List [index];
}

public Car Add(Car _car)
{
_car.CarID = List.Count + 1 ;
int index = List.Add(_car);
return (Car)List [index];
}

void 删除( int index)
{
if (index< 0 || index> List.Count-1)
throw new IndexOutOfRangeException();

List.Remove(index);
}

}





用法:


 XDocument xdoc = XDocument.Load( @  D:\ CarPaark .XML); 
Cars CarPark = new Cars(); // 汽车集合

// 从xml文档中获取数据
// 创建列表< Car>
var cars = xdoc.Descendants( Car
。选择(c = > new Car

string )c.Element( CarName),
string )c。元素( CarNumber),
string )c。元素( CarDescript ion),
int )c。元素( CarID
));
// 填充集合:从列表中添加汽车< Car>
foreach (Car c in cars)
{
CarPark.Add(c);
}

// CarPark已准备就绪,可以访问单车使用Item方法。
Car cr = CarPark.Item( 0 );
cr.CarName = Seat Toledo;





注意:您必须先对xml文件进行一些小的更改才能使用它:

 <?  xml     version   =  1.0   编码  =  utf-8    standalone   =   >  
< 汽车 >
< < span class =code-leadattribute> Car >
< CarName > BMW < / CarName >
< CarNumber > AA2935IO < / CarNumber >
< CarDescription > 汽车< < span class =code-leadattribute> / CarDescription >
< CarID > 1 < / CarID >
< / Car >
< Car >
< CarName > Mersedes < / CarName >
< CarNumber > AA2235IO < / CarNumber >
< CarDescription > Nice car < / CarDescription >
< CarID > 2 < / CarID >
< / Car >
< / Cars >





如果要实现保存方法,将所有 CarPark 数据转储到xml文件中,则必须循环执行汽车收藏:

 XDocument xdoc =  new  XDocumen吨(); 
XElement root = new XElement( 汽车);
foreach (Car c CarPark)
{
root。添加( new XElement( Car
new XElement( CarName,c.CarName),
new XElement( CarNumber,c.CarNumber),
new XElement( CarDescription,c.CarDescription),
new XElement( CarID,c.CarID)));
}
xdoc.Add(root);
xdoc.Save( @ FullPathToXmlFile.xml);


First I create DataSet object

DataSet ds = new DataSet();


Then I load xmlscheme.

ds.ReadXmlSchema("C:/Users/Levan/Dcuments/Visual Studio 2012/Projects/Crashop/CarLibrary/CarDataSet.xsd");

It load's fine.I use Dataset visualizer for debugging.Xmlscheme has following structure Table
Cars(carname(string),carnumber(string),cardiscription(string),CarID(int)).Than I try loadxml

ds.ReadXml("C:/Users/Levan/Documents/Visual Studio 2012/Projects/Crashop/CarLibrary/CarPark.xml");

xml file has following structure

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Cars>
    <carname>BMW</carname>
    <carnumber>AA2935IO</carnumber>
    <cardiscription>Nice car</cardiscription>
    <CarID>1</CarID>
  </Cars>
  <Cars>
    <carname>Mersedes</carname>
    <carnumber>AA2235IO</carnumber>
    <cardiscription>Nice car</cardiscription>
    <CarID>2</CarID>
  </Cars>
</NewDataSet>


In result no errors,but empty table.

ds.Tables["Cars"].Rows.Add("BMW","AA2935IO","Nice Car",1);

Works fine.But i want to figure out how to read from XML file.In example which i use everything good and i didn't find appropriate solution for me.

解决方案

There is - at least - few ways to read/write xml data, for example:
1. Linq to XML[^]
2. Xml Serialization/deserialization[^]

Based on this[^] i'd suggest to create custom class collection as follow:

Car class is used to store data about single car.

public class Car
{
    private string sCarName = string.Empty;
    private string sCarNumber = string.Empty;
    private string sCarDescription = string.Empty;
    private int iCarId = 0;

    //contructor
    public Car(string _CarName, string _CarNumber, string _CarDescription, int _CarId)
    {
        sCarName = _CarName;
        sCarNumber = _CarNumber;
        sCarDescription = _CarDescription;
        iCarId = _CarId;
    }

    public string CarName
    {
        get{return sCarName;}
        set{sCarName = value;}
    }

    public string CarNumber
    {
        get{return sCarNumber;}
        set{sCarNumber = value;}
    }

    public string CarDescription
    {
        get{return sCarDescription;}
        set{sCarDescription = value;}
    }

    public int CarID
    {
        get{return iCarId;}
        set{iCarId = value;}
    }

}



Cars class inherits from CollectionBase and is used to store list of cars (of type: Car):

public class Cars : CollectionBase
{

    public Car Item(int index)
    {
	if(index<0 || index>List.Count-1)
		throw new IndexOutOfRangeException();
		
	return (Car)List[index];
    }

    public Car Add(Car _car)
    {
        _car.CarID = List.Count + 1;
        int index = List.Add(_car);
        return (Car)List[index];
    }

    void Remove(int index)
    {
        if(index<0 || index>List.Count-1)
            throw new IndexOutOfRangeException();

        List.Remove(index);
    }

}



Usage:

XDocument xdoc = XDocument.Load(@"D:\CarPark.xml");
Cars CarPark = new Cars(); //collection of cars

    //fetch data from xml document
    //create List<Car>
var cars = xdoc.Descendants("Car")
        .Select(c=> new Car
        (
            (string)c.Element("CarName"),
            (string)c.Element("CarNumber"),
            (string)c.Element("CarDescription"),
            (int)c.Element("CarID")
        ));
//fill collection: add cars from List<Car>
foreach(Car c in cars)
{
    CarPark.Add(c);
}

    //CarPark is ready to use, you can access single car using Item method.
Car cr = CarPark.Item(0);
cr.CarName = "Seat Toledo";



Note: you have to make some minor changes in xml file before you'll be able to use it:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Cars>
  <Car>
    <CarName>BMW</CarName>
    <CarNumber>AA2935IO</CarNumber>
    <CarDescription>Nice car</CarDescription>
    <CarID>1</CarID>
  </Car>
  <Car>
    <CarName>Mersedes</CarName>
    <CarNumber>AA2235IO</CarNumber>
    <CarDescription>Nice car</CarDescription>
    <CarID>2</CarID>
  </Car>
</Cars>



If you want to implement Save method, which will dump all CarPark data into xml file, you have to loop through the collection of Cars:

XDocument xdoc = new XDocument();
XElement root = new XElement("Cars");
foreach(Car c in CarPark)
{
    root.Add(new XElement("Car",
                    new XElement("CarName", c.CarName),
                    new XElement("CarNumber", c.CarNumber),
                    new XElement("CarDescription", c.CarDescription),
                    new XElement("CarID", c.CarID)));
}
xdoc.Add(root);
xdoc.Save(@"FullPathToXmlFile.xml");


这篇关于无法读取DataSet的xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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