我正在重写一些使用XmlDocument来解析某些XML的代码.我想使用XmlReader来查看是否可以获得一些性能改进.XML的结构如下所示:
<items>
<item id="1" desc="one">
<itemBody date="2012-11-12" />
</item>
<item id="2" desc="two">
<itemBody date="2012-11-13" />
</item>
<item id="3" desc="three">
<itemBody date="2012-11-14" />
</item>
<item id="4" desc="four">
<itemBody date="2012-11-15" />
</item>
</items>
Run Code Online (Sandbox Code Playgroud)
基本上,我需要遍历所有<item>元素.就像我说的,旧代码的工作原理如下:
XmlDocument document = new XmlDocument();
// load XML into XmlDocument
document.LoadXml(xml);
// use xpath to split into individual item
string xPath = @"items/item";
XmlNodeList nodeList = document.SelectNodes(xPath);
// loop through each item
for (int nodeIndex = 0; nodeIndex < nodeList.Count; nodeIndex++)
{
// do something with …Run Code Online (Sandbox Code Playgroud) 我使用MVC4 Web API创建了一个RESTful Web服务.如果出现问题,我会抛出WebException.
throw new WebException("Account not found");
Run Code Online (Sandbox Code Playgroud)
这是处理异常的客户端代码:
private void webClientPost_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (e.Error == null)
{
// Display result
textBoxResult.Text = e.Result;
}
else
{
// Display status code
if (e.Error is WebException)
{
StringBuilder displayMessage = new StringBuilder();
WebException we = (WebException)e.Error;
HttpWebResponse webResponse = (System.Net.HttpWebResponse)we.Response;
displayMessage.AppendLine(webResponse.StatusDescription);
// Gets the stream associated with the response.
Stream receiveStream = webResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level …Run Code Online (Sandbox Code Playgroud) .net httpwebresponse system.net.webexception asp.net-mvc-4 asp.net-web-api