The fastest way to convert strings to XML

Hey guys, we have a question.

we have an XMLIDList helper that uses + = (:()

What you need to look for is the quickest way to do this. This was not a problem before because there were <10 elements in the lists, but we added a new function that can transfer 15k elements, and, as you can imagine, her dog is slow!

  public static string EncodeGuidListToXML(IList<Guid> elementsToEncode)
        {
            if (elementsToEncode == null || elementsToEncode.Count == 0)
                return String.Empty;

            string beginItemNode = BeginItemNode;
            string endItemNode = EndItemNode; 

            string xml = BeginRootNode;

            foreach (Guid item in elementsToEncode)
            {
                xml += beginItemNode + item.ToString().ToUpper() + endItemNode;
            }

            xml += EndRootNode;

            return xml;
        }

Thank.

+3
source share
3 answers

- , IMO. XML API. XDocument XmlDocument, . , , - , XML-API... .

, :

public static string EncodeGuidListToXml(IList<Guid> guids)
{
    if (elementsToEncode == null || elementsToEncode.Count == 0)
    {
        return "";
    }
    return new XDocument(
        new XElement("Root",
            guids.Select(guid => new XElement("Item", guid.ToString().ToUpper()))
        )).ToString();

}

, StringBuilder .

+4

StringBuilder .

" ", , ,

+7

, StringBuilder , XDocument XElement. StringBuilder , XML . :

List<string> list;
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();

protected void Page_Load(object sender, EventArgs e)
{
    list = Enumerable.Range(0, 15000).Select(i => i.ToString()).ToList();
    UsingStrings();
    UsingStringBuilder();
    UsingXDocument();
}

private void UsingStrings()
{
    timer.Reset();
    timer.Start();
    string beginItemNode = "<Node>";
    string endItemNode = "</Node>";
    string xml = "<Root>";

    foreach (string item in list)
    {
        xml += beginItemNode + item + endItemNode;
    }

    xml += "</Root>";
    timer.Stop();
    Response.Write(string.Format("Strings time:{0}<br />", timer.Elapsed.Ticks));
}

private void UsingStringBuilder()
{
    timer.Reset();
    timer.Start();
    StringBuilder sb = new StringBuilder();
    sb.Append("<Root>");

    foreach (string item in list)
    {
        sb.AppendFormat("<Node>{0}</Node>", item);
    }

    sb.Append("</Root>");
    timer.Stop();
    Response.Write(string.Format("StringBuilder time:{0}<br />", timer.Elapsed.Ticks));
}

private void UsingXDocument()
{
    timer.Reset();
    timer.Start();
    XDocument xDoc = new XDocument();
    xDoc.Add(new XElement("Root"));
    foreach (var item in list)
    {
        XElement element = new XElement("Node", item);
        xDoc.Root.Add(element);
    }
    timer.Stop();
    Response.Write(string.Format("XDocument time:{0}<br />", timer.Elapsed.Ticks));
}

:

First run:
    Strings time:239750613
    StringBuilder time:55509
    XDocument time:61904
Second run:

    Strings time:289422753
    StringBuilder time:198595
    XDocument time:80032

XML- XDocument , :

string xml = xDoc.ToString(SaveOptions.DisableFormatting);

EDIT: I initially said that XDocument works much faster, but checks several times that it is usually equivalent to the StringBuilder method, although it seems more consistent, while sometimes StringBuilder is faster and sometimes slower. Clearly, using strings is much slower in comparison.

+3
source

Source: https://habr.com/ru/post/1775373/


All Articles