Turning XML into an object with properties

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
Not sure what to use here. I have a fairly simply XML document and I'd like
to "load" it into a related class so that I can access data using class
properties, etc. I've been reading thing about "serialization", etc. but
can't quite find the right article. Maybe I'm looking at the wrong thing?

e.g. XML Doc is:
<root>
<name>Joe Smith</name>
<address>Somewhere</address>
<hobbies>
<hobby>fishing</hobby>
<hobby>walking</hobby>
</hobbies>
</root>

Load this into a class designed for this document e.g.:

AClass obj = new AClass(docAbove);
string name = obj.Name;
foreach (string hob in obj.Hobbies)...
etc.

Obviously I can do this manually by looking at nodes in the doc and writing
all the get accessors but there must be an easier way?

Thanks!
 
Thanks for Marc's input

Hi Bill,

As Mark as suggested, the most convenient approach is using .NET XML
serialization. Here are the MSDN reference about XML serialization:

#Introducing XML Serialization
http://msdn2.microsoft.com/en-us/library/182eeyhh(VS.71).aspx

#Examples of XML Serialization
http://msdn2.microsoft.com/en-us/library/58a18dwa(VS.71).aspx

The general idea of this is:

** you define a class and use some XML serialization attributes to decorate
it(control how its root class and member properties should be serialized
into XML):

**then, you can use XmlSerializer class to serialize the class instance
into XML or deserialize XML into instance of that class.

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
 
Hi - thank you both, very useful. I have that working now.

I do have a further question :)

Since I generate the serializable class using XSD.EXE, it means that if I
make slight amendments to the schema in future I'll have to regenerate it
(most of it will not change but some parts may). However, I have a number of
methods which will operate on the fields and structures stored in the object.
What's the best way of keeping these methods separate from the generated
class? Should I inherit from the generate class and keep my methods in there?
Then I'd only have to make slight changes to the inherited class to work with
the new data.

Thanks!
 
Partial Classes.

I can't remember if the xsd.exe output does this, but it should. I
don't have VS to hand, so I can't check - but ideally it should emit
code like (code file 1)
:


public partial class SomeEntity {...}

this means that you can *also* (in a separate file) declare more of
the class (code file 2):

public partial class SomeEntity {
public void SomeMethod() {...}
public event EventHandler SomeEvent {...}
...
}

The different sources are combined at compile-time to build a single
SomeEntity class. You maintain file 2, and can regenerate file 1 at
whim. In C# 3 it gets better still, with partial methods - you can
propose the skeleton in one file, and then *optionally* implement them
in a second file - for instance to add property change validation that
the generated code can invoke. If you dno't provide an implementation
it quite literally evaporates and *nothing* gets called.

If the xsd output doesn't support "partial", then inheritence isn't a
bad option. A separate static utility class might also do the job. In
a scenario like this in C# 3 a final option would be an extension
method, which is declared as static utility methods, but appear to the
editor and compiler as instance methods. This means that you can also
add extension methods to types that are outside of your control (want
a string.ToGibberish() method? fine - just add an extension).

Marc
 
Access to VS again... have verified that xsd.exe does indeed emit
"partial" classes.

Marc
 
Thanks for Marc's further input.

I agree that the "partial class" feature of .NET 2.0 is the reasonable
approach to address "automatic regenerating code" problem.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
This works really well - thanks guys!


Steven Cheng said:
Thanks for Marc's further input.

I agree that the "partial class" feature of .NET 2.0 is the reasonable
approach to address "automatic regenerating code" problem.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top