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.

7
  • Also try using StringBuilder instead of string concatenation. Commented Mar 11, 2015 at 8:52
  • XElement class can help a lot Commented Mar 11, 2015 at 8:53
  • @ASh Can you give me a sample ? Commented Mar 11, 2015 at 8:54
  • @HariPrasad string.format is a subset of the string builder class right ? Differences are there between both ? Commented Mar 11, 2015 at 8:55
  • @Dilip, description of XElement + examples: msdn.microsoft.com/en-gb/library/…. my answer for another question on SO: stackoverflow.com/questions/28745459/… Commented Mar 11, 2015 at 8:58

2 Answers 2

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; }
}
Sign up to request clarification or add additional context in comments.

6 Comments

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 ?
You can use an anonymous type, and then give this 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.
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 ?
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.
is there some tool or procedures that has to be employed to compare between two methods ?
|
0

Why not use XDocument class.

var doc = new XDocument(
                    new XElement("APPLICATION",
                        new XElement("SEND",
                            new XElement("ID", txtCardNo.Text.ToString())
                            )
                        )
                    );

3 Comments

which one would me the most memory efficient.
Practically everything will be more memory efficient when compared to base string concatenation.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.