0

I am passing lot of XML Parameters from my application to the SQL Server (both windows and ASP.Net application)

Earlier i used to build XML using the based concatenation operator in string, similar to the one below.

string XmlDetails = string.Empty;
XmlDetails = "<APPLICATION><SEND>";
XmlDetails += "<ID>" + txtCardNo.Text.ToString() + "</ID>";
XmlDetails += "</SEND></APPLICATION>";

The application really used to hog memory and was really slow. I changes the concatenation method to String Builder class to build big XML.

 XmlDetails = string.Format("{0}<{1}>{2}</{1}>", "<APPLICATION><SEND>", "ID", txtCardNo.Text.ToString()); 
 XmlDetails = string.Format("{0}<{1}>{2}</{1}>{3}", XmlDetails, "PAYDET", txtPOSPaydet.Text.ToString(), "</SEND></APPLICATION>");

While using the above method there was a drastic change in the memory levels used by my application.

I would like to know if there are any better methods which could be employed in my application.

Dilip
  • 199
  • 1
  • 2
  • 15
  • Also try using `StringBuilder` instead of string concatenation. – Hari Prasad Mar 11 '15 at 08:52
  • XElement class can help a lot – ASh Mar 11 '15 at 08:53
  • @ASh Can you give me a sample ? – Dilip Mar 11 '15 at 08:54
  • @HariPrasad string.format is a subset of the string builder class right ? Differences are there between both ? – Dilip Mar 11 '15 at 08:55
  • @Dilip, description of XElement + examples: https://msdn.microsoft.com/en-gb/library/system.xml.linq.xelement(v=vs.110).aspx. my answer for another question on SO: http://stackoverflow.com/questions/28745459/how-do-i-generate-an-html-string-from-group-by/28748923#28748923 – ASh Mar 11 '15 at 08:58
  • `String.Format` is fine, in addition to that you can modify code something like `sb.Append(string.Format("{0}<{1}>{2}{1}>", "", "ID", txtCardNo.Text.ToString()));`. – Hari Prasad Mar 11 '15 at 08:59
  • String.Format uses a stringBuilder under the hood anyways, so performance is a non-issue in that case. – Yannick Meeus Mar 11 '15 at 09:01

2 Answers2

1

There are several options available to you:

XElement, which allows you to handcraft your XML without having to build the string itself, but rather like so:

XElement xmlTree1 = new XElement("Root",
    new XElement("Child1", 1),
    new XElement("Child2", 2),
    new XElement("Child3", 3)
);

Console.WriteLine(xmlTree2);

This will write to the console:

<Root>
  <Child1>1</Child1>
  <Child2>2</Child2>
  <Child3>3</Child3>
</Root>

If your XML format is static, you can also create an object representing your data, mark it as [Serializable], then serialize it:

public static void Main(string[] args)
{
    var myObjectToSerialize = new Root()
    {
        Child1 = 1,
        Child2 = 2,
        Child3 = 3
    };

    var serializer = new XmlSerializer(typeof(Root));
    serializer.Serialize(Console.Out, myObjectToSerialize);
    Console.ReadKey();
}

With the following class:

[Serializable]
public class Root
{
    public int Child1 { get; set; }

    public int Child2 { get; set; }

    public int Child3 { get; set; }
}
Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
  • My XML is not static. It is built using different elements on the Form and other global values which could change. Can this serializer method be applied in dynamic also with dynamic variable sets ? – Dilip Mar 11 '15 at 09:15
  • You can use an anonymous type, and then give [this](http://stackoverflow.com/questions/2404685/can-i-serialize-anonymous-types-as-xml/2404984#2404984) a go. It details an extension method that should help. But if your XML structure will change depending on other factors, then I'd just recommend the XDocument/XElement route. – Yannick Meeus Mar 11 '15 at 09:19
  • Yes, I was thinking like making different serializable objects for different methods that i need. Like if i using it for a specific table then create an XML serializer for that specifc purpose and use it . But actually are there any performance impacts while using any of these methods over the existing ones which i use ? – Dilip Mar 11 '15 at 09:27
  • Personally, if the number one priorities are to generate raw XML and forgo the security blanket that the baked in XML serialization provides, then you can stick to a StringBuilder. But I'd try both, do some measuring (performance, memory, the works). If the difference is negligible, I'd highly recommend going with the native APIs. They give you so much more security when working with XML. – Yannick Meeus Mar 11 '15 at 09:41
  • is there some tool or procedures that has to be employed to compare between two methods ? – Dilip Mar 11 '15 at 10:54
  • Couldn't get much difference between the String.Format and the XElement methods. Anyhow, seemed to me that the XElement method is more correct method and hence resorted to the same. Thanks @yannickmeeus – Dilip Mar 14 '15 at 06:42
0

Why not use XDocument class.

var doc = new XDocument(
                    new XElement("APPLICATION",
                        new XElement("SEND",
                            new XElement("ID", txtCardNo.Text.ToString())
                            )
                        )
                    );
Peter
  • 483
  • 7
  • 16
  • which one would me the most memory efficient. – Dilip Mar 11 '15 at 09:16
  • Practically everything will be more memory efficient when compared to base string concatenation. – Yannick Meeus Mar 11 '15 at 09:19
  • 2
    As mentioned @YannickMeeus string concatenation is costly operation. XmlDocument or XDocument provides already API to manipulate the XML in efficient way for majority of the scenarios. – Peter Mar 11 '15 at 09:26