Solved

WCF XML serialization...

Posted on 2010-11-23
5
721 Views
Last Modified: 2013-12-17
Ill be brutally honest here, I am completely winging a project I am writing currently (as I am not too clever with WCF)... all is going well, except for this problem:

I have written a structure:

        public struct Site {
            private XmlNode _theSite;
            private string _siteName;

            public string SiteName {
                get { return _theSite.Attributes["Name"].Value; } 
            }
            public ArrayList Users {
                get {
                    if (_theSite != null) {
                        ArrayList ReturnList = new ArrayList();
                        foreach (string aUser in _theSite.Attributes["Users"].Value.Split(';')) {
                            ReturnList.Add(aUser);
                        }
                        return ReturnList;
                    } else {
                        return new ArrayList();
                    }
                }
                set {
                    if (_theSite != null) {
                        _theSite.Attributes["Users"].Value = "";
                        foreach (string aUser in value) {
                            if (_theSite.Attributes["Users"].Value == "") {
                                _theSite.Attributes["Users"].Value += aUser;
                            } else {
                                _theSite.Attributes["Users"].Value += ";" + aUser;
                            }
                        }
                        _theSite.OwnerDocument.Save(clsXMLScreens._filename);
                    }
                }
            }
            public Hashtable Screens {
                get {
                    if (_theSite != null) {
                        Hashtable ReturnList = new Hashtable();
                        foreach (XmlNode aScreen in _theSite.ChildNodes) {
                            objScreen theScreen = new objScreen(aScreen);

                            ReturnList.Add(theScreen.MACAddress, theScreen);
                        }
                        return ReturnList;
                    } else {
                        return new Hashtable();
                    }
                }
            }

            public Site(XmlNode entireSite) {
                _theSite = entireSite;
            }
        }

Open in new window


which basically privately stores an xml object, and the exposed properties of the structure will reference this xml object. So far so good...

When this structure is passed over the WCF boundary (usually in an ArrayList) all the values are empty. This is because I haven't marked the structure as "Serializable" I imagine...?

When I try to mark it Serializable, I get the frustrating WCF error that XML cannot be serialized (this is obvious when I think about it). I have read that there are ways to do this, but the cases used to describe this, involves sending the xml as a string, and then reparsing it at the other end, which obviously isn't going to work for me.

Can anyone explain how I can do this please?
0
Comment
Question by:DjDezmond
  • 3
5 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 250 total points
ID: 34198919
few suggestions before giving you the solutions. They say that one should not use structure if the size is more than 64 bytes. Struct is a value type and its really not a good idea to encapsulate Reference type itn. i would rather suggest, that convert it into a class, it will be much much more efficient.

Now coming to your problem, just go through this discussion on stackoverflow and you will get your resolution i am sure.

http://stackoverflow.com/questions/814728/what-is-the-standardized-way-to-pass-complex-types-in-wcf

hope it helps
:-)
0
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 250 total points
ID: 34200782
Agree with buggy coder a class is better.
Also replace ArrayList with List<string>. You know your array contains strings. WCF does not like serializing object types containers.
0
 
LVL 9

Author Comment

by:DjDezmond
ID: 34203953
Thanks guys, Ill convert it to a class to start with then (I did not know that by the way, so its interesting to know).

Unfortunately I am still struggling with the original issue though. I should perhaps mention that the class this structure (now a class) is embedded in, is part of my common DLL, and not the WCF service or client. I have absolutely no idea what underlying issues that causes, but all I see is that it stops me from marking it as [DataContractAttribute] and [DataMemberAttribute] respectively, although I am using the correct namespaces it seems. I have marked this type as [ServiceKnownType] in the WCF interface though...?

[ServiceContract]
public interface IService {
    
    [OperationContract]
    [ServiceKnownType(typeof(clsXMLScreens.Site))]
    ArrayList GetUserSites(string Username);
}

Open in new window


Would posting more code help?
0
 
LVL 9

Author Comment

by:DjDezmond
ID: 34206086
I have managed to sort this out by totally avoiding sending XMLObjects over the boundary (or least trying to use them on the other side). I did this by introducing a RawXML property that could be passed to an intializer on the other end to reform the object type.

Im sure there was a way to tell the serializer to allow XML objects, but as I do not know how, avoiding the issue alltogether was far easier :)

For anyone interested in the future, the modified code is below:

        public class Site {
            private XmlNode _theSite;

            public string _rawXml {
                get {return _theSite.OuterXml;}
            }

            public string SiteName {
                get { return _theSite.Attributes["Name"].Value; } 
            }

            public List<string> Users {
                get {
                    if (_theSite != null) {
                        List<string> ReturnList = new List<string>();
                        foreach (string aUser in _theSite.Attributes["Users"].Value.Split(';')) {
                            ReturnList.Add(aUser);
                        }
                        return ReturnList;
                    } else {
                        return new List<string>();
                    }
                }
                set {
                    if (_theSite != null) {
                        _theSite.Attributes["Users"].Value = "";
                        foreach (string aUser in value) {
                            if (_theSite.Attributes["Users"].Value == "") {
                                _theSite.Attributes["Users"].Value += aUser;
                            } else {
                                _theSite.Attributes["Users"].Value += ";" + aUser;
                            }
                        }
                        _theSite.OwnerDocument.Save(clsXMLScreens._filename);
                    }
                }
            }

            public Hashtable Screens {
                get {
                    if (_theSite != null) {
                        Hashtable ReturnList = new Hashtable();
                        foreach (XmlNode aScreen in _theSite.ChildNodes) {
                            objScreen theScreen = new objScreen(aScreen);

                            ReturnList.Add(theScreen.MACAddress, theScreen);
                        }
                        return ReturnList;
                    } else {
                        return new Hashtable();
                    }
                }
            }

            public Site(XmlNode entireSite) {
                _theSite = entireSite;
            }

            public Site(string RawXML) {
                XmlDocument TempReader = new XmlDocument();
                _theSite = TempReader.ReadNode(new XmlTextReader(RawXML, XmlNodeType.Element, null));
            }
        }

Open in new window


Ill split the points as you both provided tips I have used. Thanks again.
0
 
LVL 9

Author Closing Comment

by:DjDezmond
ID: 34206168
Good answers, although I ended up providing my own solution to the main problem. I had hoped that answers to my second question would have helped me to understand WCF a little more.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
New Relic recently released its Synthetics product that allows for the creation of performance monitors that periodically test a site's performance. If you wish to test an interactive workflow New Relic employs Selenium WebDriverJS to run those test…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now