费了九牛二虎之力,我成功地在 Omar's brilliant Restful Asp.net article 之后在 asp.net MVC 中创建了一个 restful 服务
只剩下一件小事。
我的 Asp.Net MVC Controller 返回一个 XML 文件,其中有这个标签
< FileCode > 24233224< / FileCode >
这是我用来发送 Get 请求的控制台应用程序,该请求为我提供整个 Xml 文件
//Generate get request
string url = "http://localhost:1193/Home/index?File=343456789012286";
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
GETRequest.Method = "GET";
GETRequest.ContentType = "text/xml";
GETRequest.Accept = "text/xml";
Console.WriteLine("Sending GET Request");
HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
Stream GETResponseStream = GETResponse.GetResponseStream();
StreamReader sr = new StreamReader(GETResponseStream);
Console.WriteLine("Response from Server");
// This writes whole file on screen
Console.WriteLine(sr.ReadToEnd());
我可以保存这个文件,然后使用 Linq 来解析它,但是我不能不保存它就直接取出我的标签中的值吗?我只需要 FileCode
谢谢你:)
最佳答案
您可以使用 XPathReader (source download) .
它带有源代码和测试套件。
它为您提供了在流模式下使用高级查询结构 (XPath) 的能力。
CodeProject上也有类似的文章:Fast screen scraping with XPath over a modified XmlTextReader and SgmlReader
关于c# - 从流式 XML 文件中解析并提取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642711/