Parsing XML

  • Thread starter Thread starter Fabio Cannizzo
  • Start date Start date
F

Fabio Cannizzo

I have an XML file based on an XSD schema.

It represents the content of a treeview (or a menu system), where the nodes
are always of the same few types, but can be arbitrarily nested.

I am writing my own recursive parser to read the file, but I wonder if there
is some method ready to use provided by the .NET which does the job!

Thanks
 
Surely, System.Xml namespace includes too much functionality for this.
DataSet.ReadXML is the easiest way...
 
If I use DataSet, then I will still have to analyse the recursive structure
of the DataSet in order to create my Menu. Am I correct?
Thanks,
Fabio
 
It depends. If you design your schema as a relation table. You would use
two(count of distinct tags) data table. Just think as a relation database.
One entity is Orders and others is OrderDetails. Same way, Menu and Submenu.
One table should contain main menus. Other one should contain submenu.. It
is similar to tree implementation in a database.

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET
 
I would suggest serializing to an arraylist, then use the arraylist to
populate your treeview. The advantage to this is that you can preserve
the structure of your nodes and read it back without any specialized
algorithms or parsers

John Chaffier
 
You thought about generating classes from the schema using XSD.EXE (part of the SDK) and then using the generated classes to deserialize the document using the XmlSerializer?

This would give you an object graph to recurse into which should be easier.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

If I use DataSet, then I will still have to analyse the recursive structure
of the DataSet in order to create my Menu. Am I correct?
Thanks,
Fabio

Yunus Emre ALP?ZEN said:
Surely, System.Xml namespace includes too much functionality for this.
DataSet.ReadXML is the easiest way...

--

Thanks,
Yunus Emre ALP?ZEN
BSc, MCAD.NET



[microsoft.public.dotnet.languages.csharp]
 
Thanks guys.
Lots of good idea here.
Regarding Array serialization in particular, I guess for each entry I would
need to save the index in the array of the parent node, or something like
that, correct?
Thanks,
Fabio
 
Richard said:
You thought about generating classes from the schema using XSD.EXE (part of the SDK) and then using the generated classes to deserialize the document using the XmlSerializer?

This would give you an object graph to recurse into which should be easier.
[I agree this is a good idea in most cases, but for some complicated
schema, actually, it won't generate correct classes to handle that , and
will report some encrypted error message which at least misleads me. ]
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


If I use DataSet, then I will still have to analyse the recursive structure
of the DataSet in order to create my Menu. Am I correct?
Thanks,
Fabio

Yunus Emre ALP?ZEN said:
Surely, System.Xml namespace includes too much functionality for this.
DataSet.ReadXML is the easiest way...

--

Thanks,
Yunus Emre ALP?ZEN
BSc, MCAD.NET



[microsoft.public.dotnet.languages.csharp]
 
It may be even easier than that. Lets say this is what your Xml would
look like

<xml>
<ArrayRoot>
<Node level="1">Text</Node>
<Node level="1">Text2</Node>
<Node level="2">Text2a</Node>
<Node level="2">Text2b</Node>
</ArrayRoot>
</xml>

You would set up an arraylist of nodes, each node element contains the
text that is to be displayed in the treeview, the level attribute is
indicating it's depth in the tree. Now you can use a simple foreach loop
to traverse the arraylist to build the tree. The code would have to be
responsible to knowing an incoming nodes parent, so it would be
interesting to write using a recursive algorithm.

Hope this helps
John Chaffier
 
Back
Top