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

发布于 2024-12-07 03:43:59 字数 640 浏览 1 评论 0原文

我只是使用此代码从 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());

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

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最舍不得你 2024-12-14 03:43:59
public XmlDocument document = new XmlDocument();
document.LoadXml(ds.GetXml());
XmlNode ht = document.DocumentElement.SelectSingleNode("/Schedule/AudioVedioPlayer/Height");
public XmlDocument document = new XmlDocument();
document.LoadXml(ds.GetXml());
XmlNode ht = document.DocumentElement.SelectSingleNode("/Schedule/AudioVedioPlayer/Height");
荭秂 2024-12-14 03:43:59

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

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

DataSet: Schedule
表:音频视频播放器
属性:高度

如果我对您的设置正确,那么您可以通过深入研究数据集轻松获得所需的值。

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

然后您可以访问该表:

ScheduleTable table = ds.Schedule;

并获取值

int height = table.Height;

或者您可以直接使用它

int height = ds.Schedule.Height;

没有理由获取 XML,除非您想要 XML 的整个部分。

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.

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:

DataSet: Schedule
Table: AudioVedioPlayer
Property: Height

If I am correct about your setup, then you can easily get to the value you require by drilling down into the 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文