Passing Parameters as Xml to a Stored Procedure

后端 未结 4 1324
庸人自扰
庸人自扰 2020-12-29 15:34

I\'ve got a requirement to pass parameters as Xml to my stored procedures.

I have a WCF service in the middle tier that makes calls to my data layer which in turn fo

4条回答
  •  独厮守ぢ
    2020-12-29 16:12

    You could just use an object Serialization class like this

     public class Serialization
        {
            /// 
            /// Serializes the object.
            /// 
            /// My object.
            /// 
            public static XmlDocument SerializeObject(Object myObject)
            {
                XmlDocument XmlObject = new XmlDocument();
                String XmlizedString = string.Empty;
    
                try
                {                
                    MemoryStream memoryStream = new MemoryStream();
                    XmlSerializer xs = new XmlSerializer(myObject.GetType());
                    XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                    xs.Serialize(xmlTextWriter, myObject);
                    memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                    XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());                
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e);
                }
                XmlObject.LoadXml(XmlizedString);
                return XmlObject;            
            }
    
            /// 
            /// Deserializes the object.
            /// 
            /// 
            /// The p xmlized string.
            /// 
            public static T DeserializeObject(String XmlizedString)
            {
                XmlSerializer xs = new XmlSerializer(typeof(T));
                MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(XmlizedString));
                //XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                Object myObject = xs.Deserialize(memoryStream);
                return (T)myObject;
            } 
    
            /// 
            /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
            /// 
            /// Unicode Byte Array to be converted to String
            /// String converted from Unicode Byte Array
            private static String UTF8ByteArrayToString(Byte[] characters)
            {
                UTF8Encoding encoding = new UTF8Encoding();
                String constructedString = encoding.GetString(characters);
                return (constructedString);
            }
    
    
    
            /// 
            /// Converts the String to UTF8 Byte array and is used in De serialization
            /// 
            /// 
            /// 
            private static Byte[] StringToUTF8ByteArray(String pXmlString)
            {
                UTF8Encoding encoding = new UTF8Encoding();
                Byte[] byteArray = encoding.GetBytes(pXmlString);
                return byteArray;
            } 
        }
    

    then you don't have to build the XML by hand, plus you can use this with any item to transform it using XSLT

提交回复
热议问题