Read XML file easily with LINQ to XML
2011-10-27 22:27
435 查看
/*By Jiangong SUN*/
How to read XML file easily?
1. You can get your stream with a given url.
2. Then get your XElement you want using LINQ to XML request:
3. Then you can treat the data and use it your application.
A simple example:
Enjoy coding!
How to read XML file easily?
1. You can get your stream with a given url.
private string GetStreamString(string url) { try { WebRequest request = WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); return streamRead.ReadToEnd(); } catch (Exception e) { Log.Error(e); return null; } }
2. Then get your XElement you want using LINQ to XML request:
private static IEnumerable<XElement> GetElements(string streamString, string matchName) { if (!string.IsNullOrEmpty(streamString)) { XDocument xml = XDocument.Parse(streamString); IEnumerable<XElement> d = (from x in xml.Root.Elements(matchName) select x); return d; } else { return null; } }
3. Then you can treat the data and use it your application.
A simple example:
RList = (from range in Ranges.Elements("node").Elements("subnode") select range).ToList();
Enjoy coding!
相关文章推荐
- Error:Failed to load project configuration:xxxxxxxxxxxxxxxxxxxx cannot read file .idea/misc.xml
- A simple way to read an XML file in Java
- How to read a PCap file from Wireshark with C++
- How to mount partition with ntfs file system and read write access
- (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML
- Problem with generating association inside dbml file for LINQ to SQL
- How to read a PCap file from Wireshark with C++
- How To Read XML File In Java – (SAX Parser)
- AWS S3对象无法下载——This XML file does not appear to have any style information associated with it
- SVN :This XML file does not appear to have any style information associated with it.
- How To Read and Write XML Documents with GDataXML
- Step By Step guide to Read XML file in Java Using SAX Parser Example
- How to Parse or Read XML File in Java >> XML Tutorial Example
- IDEA | Error:Failed to load project configuration:xxx cannot read file .idea/misc.xml
- [Tips and Tricks] LINQ to XML with xmlns
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- How To Read and Write XML Documents with GDataXML(使用GData读写XML文档)
- about how to read the complicated xml file
- How to mount partition with ntfs file system and read write access
- Use Jdom to Read XML file