[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 738
  • Last Modified:

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?
0
DjDezmond
Asked:
DjDezmond
  • 3
2 Solutions
 
BuggyCoderCommented:
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
 
Miguel OzSoftware EngineerCommented:
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
 
DjDezmondAuthor Commented:
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
 
DjDezmondAuthor Commented:
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
 
DjDezmondAuthor Commented:
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

When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot has fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now