c# – 使用StringBuilder编写XML好吗?
发布时间:2020-12-15 17:46:20 所属栏目:百科 来源:网络整理
导读:感觉很脏但也许不是…使用StringBuilder编写 XML可以吗?我的直觉本能就是说“虽然这种感觉很错,但是由于没有加载额外的库和开销,因此它不会执行任何额外的XmlWriter调用方法.它似乎一般只是代码少一些. XmlWriter有什么好处? 这是它的样子.我正在根据您所
感觉很脏但也许不是…使用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的.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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- C#通过创建Windows服务启动程序的方法详解
- SQLite 附加数据库(http://www.w3cschool.cc/sqlite/sqlite
- xml解析-dom范例
- Analytic Functions in Oracle
- Bison / Flex解??析器将字符串作为标记值
- objective-c – NSDate到GMTDate到JSON格式
- Moya源码解析
- Cannot create resource output directory
- actionscript-3 – Adob??e AIR – .air到.exe / .dmg / .r
- Flash Builder 4 安装报错 Unable to get root from inChil