[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

WCF XML serialization...

Posted on 2010-11-23
5
Medium Priority
?
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 750 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 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 750 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

650 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