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

前端之家收集整理的这篇文章主要介绍了c# – 使用StringBuilder编写XML好吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感觉很脏但也许不是…使用StringBuilder编写 XML可以吗?我的直觉本能就是说“虽然这种感觉很错,但是由于没有加载额外的库和开销,因此它不会执行任何额外的XmlWriter调用方法.它似乎一般只是代码少一些. XmlWriter有什么好处?

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

  1. public void ProcessRequest(HttpContext context)
  2. {
  3. context.Response.ContentType = "text/xml";
  4.  
  5. string domain = WebUtils.ReturnParsedSourceUrl(null); //returns something like www.sample.com
  6. string cachedChan = context.Cache[domain + "_opensearchdescription"] as String;
  7.  
  8. if (cachedChan == null)
  9. {
  10. StringBuilder sb = new StringBuilder();
  11. sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  12. sb.Append("<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">");
  13. sb.Append(" <ShortName>Search</ShortName>");
  14. sb.Append(" <Description>Use " + domain + " to search.</Description>");
  15. sb.Append(" <Contact>contact@sample.com</Contact>");
  16. sb.Append(" <Url type=\"text/html\" method=\"get\" template=\"http://" + domain + "/Search.aspx?q={searchTerms}\" />");
  17. sb.Append(" <moz:SearchForm>http://" + domain + "/Search.aspx</moz:SearchForm>");
  18. sb.Append(" <Image height=\"16\" width=\"16\" type=\"image/x-icon\">http://" + domain + "/favicon.ico</Image>");
  19. sb.Append("</OpenSearchDescription>");
  20.  
  21. cachedChan = sb.ToString();
  22.  
  23. context.Cache.Insert(domain + "_opensearchdescription",cachedChan,null,DateTime.Now.AddDays(14),TimeSpan.Zero);
  24. }
  25.  
  26. context.Response.Write(cachedChan);
  27. }

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

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

解决方法

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

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

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

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

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

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

猜你在找的C#相关文章