Link to home
Start Free TrialLog in
Avatar of DjDezmond
DjDezmondFlag for United Kingdom of Great Britain and Northern Ireland

asked on

WCF XML serialization...

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?
ASKER CERTIFIED SOLUTION
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DjDezmond

ASKER

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?
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.
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.