微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用StringBuilder编写XML好吗?

感觉很脏但也许不是…使用StringBuilder编写 XML可以吗?我的直觉本能就是说“虽然这种感觉很错,但是由于没有加载额外的库和开销,因此它不会执行任何额外的XmlWriter调用方法.它似乎一般只是代码少一些. XmlWriter有什么好处?

这是它的样子.我正在根据您所在的域构建OpenSearch XML文档.

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/xml";

    string domain = WebUtils.ReturnParsedSourceUrl(null); //returns something like www.sample.com
    string cachedChan = context.Cache[domain + "_opensearchdescription"] as String;

    if (cachedChan == null)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        sb.Append("<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">");
        sb.Append("    <ShortName>Search</ShortName>");
        sb.Append("    <Description>Use " + domain + " to search.</Description>");
        sb.Append("    <Contact>contact@sample.com</Contact>");
        sb.Append("    <Url type=\"text/html\" method=\"get\" template=\"http://" + domain + "/Search.aspx?q={searchTerms}\" />");
        sb.Append("    <moz:SearchForm>http://" + domain + "/Search.aspx</moz:SearchForm>");
        sb.Append("    <Image height=\"16\" width=\"16\" type=\"image/x-icon\">http://" + domain + "/favicon.ico</Image>");
        sb.Append("</OpenSearchDescription>");

        cachedChan = sb.ToString();

        context.Cache.Insert(domain + "_opensearchdescription",cachedChan,null,DateTime.Now.AddDays(14),TimeSpan.Zero);
    }

    context.Response.Write(cachedChan);
}

跟随,〜2年后
我意识到我的意思是说,完全没有说出来:使用XML类生成这个文件代码的好处是什么,而不是使用字符串?有没有这比(例如)约翰·桑德的例子更糟吗?

我使用吉姆舒伯特的方法,选择“我可以读这个,这是有道理的”,而不是争取“正确性”.我很高兴我做到了约翰·桑德的例子没有什么不对,但是我觉得这对于我想要完成的事情来说太过分了.实用主义?也许.

解决方法

这是非常错误的使用一个理解XML的.NET API来编写XML.

使用System.Xml.XmlWriter不会通过加载“任何额外的库”导致任何性能问题.

使用XML API的原因是他们了解XML的规则.例如,他们会知道一个元素中需要引用的字符集,以及需要在属性中引用的不同集合.

这可能不是您的案例中的一个问题:也许您确定域不会有任何需要引用的字符.在任何更广泛的情况下,最好让XML API做XML,他们知道如何做 – 所以你不必自己去做.

以下是使用LINQ to XML生成有效XML的简单方法

public static string MakeXml()
{
    XNamespace xmlns = "http://a9.com/-/spec/opensearch/1.1/";
    XNamespace moz = "http://www.mozilla.org/2006/browser/search/";
    string domain = "http://localhost";
    string searchTerms = "abc";
    var doc = new XDocument(
        new XDeclaration("1.0","UTF-8","yes"),new XElement(
            xmlns + "OpenSearchDescription",new XElement(xmlns + "ShortName","Search"),new XElement(
                xmlns + "Description",String.Format("Use {0} to search.",domain)),new XElement(xmlns + "Contact","contact@sample.com"),new XElement(
                xmlns + "Url",new XAttribute("type","text/html"),new XAttribute("method","get"),new XAttribute(
                    "template",String.Format(
                        "http://{0}/Search.aspx?q={1}",domain,searchTerms))),new XElement(
                moz + "SearchForm",String.Format("http://{0}/Search.aspx",new XElement(
                xmlns + "Image",new XAttribute("height",16),new XAttribute("width","image/x-icon"),String.Format("http://{0}/favicon.ico",domain))));
    return doc.ToString(); // If you _must_ have a string
}

原文地址:https://www.jb51.cc/csharp/96899.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐