Passing Parameters as Xml to a Stored Procedure

后端 未结 4 1355
庸人自扰
庸人自扰 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:28

    If you must use xml; then rather than passing around a dictionary, I'd use a class that represents that data, and use XmlSerializer to fetch it as xml:

    [Serializable, XmlRoot("args")]
    public class SomeArgs {
        [XmlElement("foo")] public string Foo { get; set; } 
        [XmlAttribute("bar")] public int Bar { get; set; }
    }
    ...
    SomeArgs args = new SomeArgs { Foo = "abc", Bar = 123 };
    XmlSerializer ser = new XmlSerializer(typeof(SomeArgs));
    StringWriter sw = new StringWriter();
    ser.Serialize(sw, args);
    string xml = sw.ToString();
    

    This makes it much easier to manage which arguments apply to which queries, in an object-oriented way. It also means you don't have to do your own xml escaping...

提交回复
热议问题