如何从C#中的DATASET对象读取xml数据
问题描述:
我刚刚使用此代码从XML文件读取XML数据。它成功从file.I读取数据。我只是想知道如何从XML读取SelectSingleNode数据数据集。如何从C#中的DATASET对象读取xml数据
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数据,并将其存储到一个变量。
答
public XmlDocument document = new XmlDocument();
document.LoadXml(ds.GetXml());
XmlNode ht = document.DocumentElement.SelectSingleNode("/Schedule/AudioVedioPlayer/Height");
答
您可以将数据集中的xml写入流中,然后使用该流将其读入到XmlDocument对象中。从那里,你可以使用DOM来导航。
现在,您是否试图将一部分数据(如XML)存储到变量中?或者你在寻找身高值?如果前者,我对你的计划没有任何问题。如果是后者,我认为,从看你的XPath语句(的SelectSingleNode()),您具备以下条件:
数据集:计划 表:AudioVedioPlayer 物业:身高
如果我对您的设置正确,那么通过向下钻取到DataSet中,您可以轻松获得所需的值。
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 。
thnx4 help,bt我可以从数据集中读取数据... ?? – kaushik
编辑为直接从DataSet中读取。 –
THnxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...刚刚应用于演示应用程序。正在添加到大one.wish我的运气.... n也sorrry f我的thnx似乎对你来说很小......我真的意味着它的大方式。 – kaushik