Jump to content
  • Advertisement
Sign in to follow this  
Sijmen

[.net] Reading XML

This topic is 5407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there! I would like to read an XML file, for example:
<?xml version="1.0">
<books>
 <book>
  <title>.NET For Dummies</title>
  <author>Doc. Net</author>
 </book>
 <book>
  <title>XML For Dummies</title>
  <auther>C.U. There</author>
 </book>
</books>

The problem is, I don't know how to parse it. This is how I would love it:
XMLReader reader = new XMLReader(fileStreamOfXmlFile);
Node[] top = reader.GetTopNode().GetNodes("book");

foreach(Node book in top)
{
	Console.WriteLine("Title:  {0}", book.GetNodes("title")[0];
	Console.WriteLine("Author: {1}", book.GetNodes("author")[0];
}

Do such classes exist in the .NET Framework? There are so many classes that I can't see the wood trough the trees. If there aren't such classes, what would be the best other way to do such parsing? Thanks beforehand!

Share this post


Link to post
Share on other sites
Advertisement
I generally load the document into an XmlDocument and parse that.


XmlDocument xmlDoc = New XmlDocument();

xmlDoc.Load( "yourfilename.xml" );
XmlElement xmlRoot = xmlDoc.DocumentElement;
XmlNodeList xmlBookNodes = xmlRoot.GetElementsByTagName("book");

foreach (XmlElement xmlBook in xmlBookNodes)
{

// Do your stuff

}




I've not used XmlReaders much so my way makes sense to me and works pretty well for my needs. But then, there's always so many ways to skin a cat in .NET.

Share this post


Link to post
Share on other sites
I would make an xml schema and a dataset that is typed to the xml schema. I would then use an xmlreader with my dataset to read the elements.

Share this post


Link to post
Share on other sites
Yup. I think xGoblin's got it right. There's even a snazzy integrated way of doing it (in Visual Studio) that involves little coding. Create an XML "Schema" and turn on GenerateDataset. It will generate a nice class for you that will load an XML file using that schema.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by The Frugal Gourmet
Yup. I think xGoblin's got it right. There's even a snazzy integrated way of doing it (in Visual Studio) that involves little coding. Create an XML "Schema" and turn on GenerateDataset. It will generate a nice class for you that will load an XML file using that schema.


I don't think I did any coding. I made the xml schema in the editor, and then on the .aspx page, I dragged a dataset over it, told it to make it a typed dataset in the drop-down menu (specifying the correct type of course), and then calling the readXML function, which is basically the only coding I had to do.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!