views:

84

answers:

3

I'm developing a system to pick up XML attachments from emails, via Exchange Web Services, and enter them into a DB, via a custom DAL object that I've created.

I've manage to extract the XML attachment and have it ready as a stream... they question is how to parse this stream and populate a DAL object.

I can create an XMLTextReader and iterate through each element. I don't see any problems with this other than that I suspect there is a much slicker way. The reader seems to treat the opening tag, the content of the tag and the closing tag as different elements (using reader.NodeType). I expected myValue to be considered one element rather than three. Like I said, I can get round this problem, but I'm sure there must be a better way.

I came across the idea of using an XML Serializer (completely new to me) but a quick look suggested that these can't handle ArrayLists and List (I'm using List).

Again, I'm new to LINQ, but LINQ-to-XML has also been mentioned, but examples I've seen seem rather complex - though that my simply be my lack of familiarity.

Basically, I don't want a cludged system, but I don't want to use any complicated technique with a learning curve, just because it's 'cool'.

What is the simplest and most effective way of translating this XML/Stream in to my DAL objects?

XML Sample:

<?xml version="1.0" encoding="UTF-8"?>
<enquiry>
    <enquiryno>100001</enquiryno>
    <companyname>myco</companyname>
    <typeofbusiness>dunno</typeofbusiness>
    <companyregno>ABC123</companyregno>
    <postcode>12345</postcode>
    <contactemail>[email protected]</contactemail>
    <firstname>My</firstname>
    <lastname>Name</lastname>
    <vehicles>
        <vehicle>
            <vehiclereg>54321</vehiclereg>
            <vehicletype>Car</vehicletype>
            <vehiclemake>Ford</vehiclemake>
            <cabtype>n/a</cabtype>
            <powerbhp>130</powerbhp>
            <registrationdate>01/01/2003</registrationdate>
        </vehicle>
    </vehicles>
</enquiry>

Update 1: I'm trying to deserialize, based on Graham's example. I think I've set up the DAL for serialization, including specifying [XmlElement("whatever")] for each property. And I've tried to deserialize using the following:

SalesEnquiry enquiry = null;
XmlSerializer serializer = new XmlSerializer(typeof(SalesEnquiry));
enquiry = (SalesEnquiry)serializer.Deserialize(stream);

However, I get an exception:'There is an error in XML document (2, 2)'. The innerexception states {"<enquiry xmlns=''> was not expected."}

Conclusion (updated):

My previous problem was the fact that the element in the XML file (Enquiry) != the name of the class (SalesEnquiry). Rather than an [XmlElement] attribute for the class, we need an [XmlRoot] attribute instead. For completeness, if you want a property in your class to be ignored during serialization, you use the [XmlIgnore] attribute.

I've successfully serialized my object, and have now successfully taken the incoming XML and de-serialized it into a SalesEnquiry object.

This approach is far easier than manually parsing the XML. OK, there has been a steep learning curve, but it was worth it.

Thanks!

+5  A: 

If your XML uses a schema (i.e. you're always going to know what elements appear, and where they appear in the tree), you could use XmlSerializer to create your objects. You'd just need some attributes on your classes to tell the serializer what XML elements or attributes they correspond to. Then you just load up your XML, create a new XmlSerializer with the type of the .NET object you want to create, and call the Deserialize method.

For example, you have a class like this:

[Serializable]
public class Person
{
    [XmlElement("PersonName")]
    public string Name { get; set; }

    [XmlElement("PersonAge")]
    public int Age { get; set; }

    [XmlArrayItem("Child")]
    public List<string> Children { get; set; }
}

And input XML like this (saved in a file for this example):

<?xml version="1.0"?>
<Person>
  <PersonName>Bob</PersonName>
  <PersonAge>35</PersonAge>
  <Children>
    <Child>Chris</Child>
    <Child>Alice</Child>
  </Children>
</Person>

Then you create a Person instance like this:

Person person = null;
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (FileStream fs = new FileStream(GetFileName(), FileMode.Open))
{
    person = (Person)serializer.Deserialize(fs);
}

Update: Based on your last update, I would guess that either you need to specify an XmlRoot attribute on the class that's acting as your root element (i.e. SalesEnquiry), or the XmlSerializer might be a bit confused that you're referencing an empty namespace in your XML (xmlns='' doesn't seem right).

Graham Clark
Once I get this working (see above), this should be extremely handy, thanks.
CJM
I tried specifying a XmlElement attribute for the class, but it didn't allow it. Also I tried add a namespace to the enquiry element, and the same error occured at the same point. I'm stumped.
CJM
@CJM, sorry, my mistake: it's not `XmlElement` you need at the class level, but `XmlRoot`. I've updated my answer.
Graham Clark
Noted - I've updated my conclusions to include XmlRoot and XmlIgnore... Thanks.
CJM
+2  A: 

XmlSerializer does support arrays & lists... as long as the contained type is serializable.

tenfour
+1  A: 

I have found Xsd2Code very helpful for this kind of thing: http://xsd2code.codeplex.com/

Basically, all you need to do is write an xsd file (an XML schema file) and specify a few command line switches. Xsd2Code will automatically generate a C# class file that contains all the classes and properties plus everything needed to handle the serialization. It's not a perfect solution as it doesn't support all aspects of XSD, but if your XML files are relatively simple collections of elements and attributes, it should be a nice short-cut for you.

There's another similar project on Codeplex called Linq to XSD (http://linqtoxsd.codeplex.com/), which was designed to enforce the entire XSD specification, but last time I checked, it was no longer being supported and not really ready for prime time. Thought it was worth a mention, though.

DanM
Thanks Dan - might have been useful earlier, but for whatever reason, I created the DAL then linked back to the XML. If this app does it's stuff, it might have shortcut the process even further. On the other hand, it's been a valuable learning exercise as it is...
CJM