开发者

how to read xml data from a DATASET object in c#

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("/S开发者_JS百科chedule/AudioVedioPlayer/Height");

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜