如何从c#中的DATASET对象读取xml数据

时间:2022-11-29 19:52:04

I just used this code to read xml data from an XML file.It successfully reads the data from file.I Just want to know how can I read SelectSingleNode data from an xml dataset.

我刚刚使用此代码从XML文件中读取xml数据。它成功从文件中读取数据。我只想知道如何从xml数据集中读取SelectSingleNode数据。

public DataSet ds =new DataSet();
public FileStream stream = null;

stream = new FileStream(schdlpath, FileMode.Open);
ds.ReadXml(stream);

//after this even if i delete the xml file it won't hamper the appliaction.
stream.Close();

//this way I can print all the data..
Console.WriteLine(ds.GetXml());

All I want is to read the data like this

我想要的只是读取这样的数据

public XmlDocument document = new XmlDocument();
XmlNode ht = document.DocumentElement.SelectSingleNode("/Schedule/AudioVedioPlayer/Height");

Actually I want to read the xml data and store it into a variable.

实际上我想读取xml数据并将其存储到变量中。

2 个解决方案

#1


1  

public XmlDocument document = new XmlDocument();
document.LoadXml(ds.GetXml());
XmlNode ht = document.DocumentElement.SelectSingleNode("/Schedule/AudioVedioPlayer/Height");

#2


0  

You can write the xml from the dataset into a stream and then use the stream to read it into an XmlDocument object. From there, you can use the DOM to navigate.

您可以将数据集中的xml写入流中,然后使用该流将其读入XmlDocument对象。从那里,您可以使用DOM进行导航。

Now, are you trying to store a part of the data, as XML, into a variable? Or are you looking for the height value? If the former, I have no issue with your plan. If the later, I assume, from looking at your XPath statement (SelectSingleNode()) that you have the following:

现在,您是否尝试将一部分数据(如XML)存储到变量中?或者你在寻找身高值?如果是前者,我的计划没有问题。如果后者,我认为,通过查看您的XPath语句(SelectSingleNode()),您有以下内容:

DataSet: Schedule Table: AudioVedioPlayer Property: Height

数据集:计划表:AudioVedioPlayer属性:高度

If I am correct about your setup, then you can easily get to the value you require by drilling down into the DataSet.

如果我对您的设置是正确的,那么您可以通过向下钻取到DataSet轻松获得所需的值。

public DataSet ds =new DataSet(); 
public FileStream stream = null;
stream = new FileStream(schdlpath, FileMode.Open); 
ds.ReadXml(stream);

You can then get to the table:

然后你可以到桌子:

ScheduleTable table = ds.Schedule;

And get the value

并获得价值

int height = table.Height;

Or you can just shortcut it

或者你可以快捷方式

int height = ds.Schedule.Height;

No reason to get XML unless you want an entire section of XML.

没有理由获得XML,除非你想要整个XML部分。

#1


1  

public XmlDocument document = new XmlDocument();
document.LoadXml(ds.GetXml());
XmlNode ht = document.DocumentElement.SelectSingleNode("/Schedule/AudioVedioPlayer/Height");

#2


0  

You can write the xml from the dataset into a stream and then use the stream to read it into an XmlDocument object. From there, you can use the DOM to navigate.

您可以将数据集中的xml写入流中,然后使用该流将其读入XmlDocument对象。从那里,您可以使用DOM进行导航。

Now, are you trying to store a part of the data, as XML, into a variable? Or are you looking for the height value? If the former, I have no issue with your plan. If the later, I assume, from looking at your XPath statement (SelectSingleNode()) that you have the following:

现在,您是否尝试将一部分数据(如XML)存储到变量中?或者你在寻找身高值?如果是前者,我的计划没有问题。如果后者,我认为,通过查看您的XPath语句(SelectSingleNode()),您有以下内容:

DataSet: Schedule Table: AudioVedioPlayer Property: Height

数据集:计划表:AudioVedioPlayer属性:高度

If I am correct about your setup, then you can easily get to the value you require by drilling down into the DataSet.

如果我对您的设置是正确的,那么您可以通过向下钻取到DataSet轻松获得所需的值。

public DataSet ds =new DataSet(); 
public FileStream stream = null;
stream = new FileStream(schdlpath, FileMode.Open); 
ds.ReadXml(stream);

You can then get to the table:

然后你可以到桌子:

ScheduleTable table = ds.Schedule;

And get the value

并获得价值

int height = table.Height;

Or you can just shortcut it

或者你可以快捷方式

int height = ds.Schedule.Height;

No reason to get XML unless you want an entire section of XML.

没有理由获得XML,除非你想要整个XML部分。