1

I have a very simple application. MainUI has the List of CustomClass. I pass this list to WCF service. WCF Service further needs to save these objects in database.

I am using Open XML in our sql stored procedure to get better performnace. But i don't know how to convert my List of Objects to XML.

If i have a datatable, it'll be easy as datatables have methods to get the XML out of them. But how to use for List of objects.

I completly understand that if my List is coming over the WCF, it is getting serialized properly. But what should i exactly need to do.


Thera are two ways: use XmlSerializer or DataContractSerializer.

4

3 回答 3

1

IMO, look into Controlling XML Serialization with Attributes and the XmlSerializer class and possibly create container classes parallel to your CustomClass. While List<> can't be automatically serialized by the default XML serializer, an array can be.

于 2012-05-27T14:03:46.847 回答
1

Code for convert list to xml : List name GridDetails

 void ConvertToXml()
        {

            string xmlString = ConvertObjectToXMLString(GridDetails);
            // Save C# class object into Xml file
            XElement xElement = XElement.Parse(xmlString);
            xElement.Save(@"C:\Users\user\Downloads\userDetail.xml");
        }


 static string ConvertObjectToXMLString(object classObject)
    {
        string xmlString = null;
        XmlSerializer xmlSerializer = new XmlSerializer(classObject.GetType());
        using (MemoryStream memoryStream = new MemoryStream())
        {
            xmlSerializer.Serialize(memoryStream, classObject);
            memoryStream.Position = 0;
            xmlString = new StreamReader(memoryStream).ReadToEnd();
        }
        return xmlString;
    }
于 2016-04-01T11:27:33.857 回答
1

Thera 有两种方法:使用XmlSerializerDataContractSerializer

于 2012-05-27T13:50:50.383 回答