How to quickly process xml file information

Source: Internet
Author: User

This example is for a friend who writes a processing code at a speed of 1 pen/0.3 seconds, and I re-write the code to run the data of 1000 pens/0.3 seconds. The speed is improved by 1000 times. The Code is as follows.

------------------------------------------------------------------ 1. Pending xml string data partition <Positions from = "51Job"> <Company> <CorpName> <! [CDATA [Beijing XXXX Ltd.]> </CorpName> <OfficeAddr1> <! [CDATA [XXX floor, block XXX, XXX building, Haidian District, Beijing]> </OfficeAddr1> <OffPostCode1> <! [CDATA [100086]> </OffPostCode1> <CompanySize> <! [CDATA [XXX or above]> </CompanySize> <TradeCodes> <TradeCode> <! [CDATA [office supplies and equipment]> </TradeCode> <! [CDATA [computer hardware]> </TradeCode> </TradeCodes> <AboutUs> <! [CDATA [XXX digital office company was founded in xxxx-xxxx. After years of development, it has become a XXXX-centered National Joint-Stock Company with XX specialized companies, it is distributed in core cities such as Beijing, Shanghai, Guangzhou, Wuhan, Chengdu, Xi'an, Changsha, and Nanchang, with a registered capital of 50 million yuan and nearly 500 employees. Adhering to the business philosophy of "advanced technology, high-quality products, perfect services, and reasonable prices", since the founding of the company, the company's sales, Technical Services, R & D and other high-quality talents provide customers from all walks of life with demand analysis, product Consulting, professional training, network construction, software development, software application training and maintenance, hardware sales, maintenance and maintenance, supply of various consumables, etc. One-Stop customized professional, standardized, and humanized services. Taihe digital office enterprises shall attach importance to training talents, implement the human resources policy of "Ren weixian", and actively cultivate a modern workforce with ideals, ethics, sense of responsibility, and collaboration spirit, at the same time, we actively introduce advanced management concepts and adopt standardized and scientific management methods to continuously expand our own strength. The company always adheres to the concept of joint development of talents and enterprises, pays attention to the skills training and development of employees, and constantly strengthens the construction of corporate culture. The continuous and rapid development of the company's business also provides great development space for its employees. The diversified corporate culture and people-oriented management philosophy prove Taihe is a good development platform. Company Website http://www.XXXX.com.cn]> </AboutUs> <Websites> <Website> <! [CDATA [http://www.techhero.com.cn]> </Website> </Websites> <Informations> <Information> <Contact> <! [CDATA [Human Resources Department]> </Contact> <Addr> <! [CDATA [Layer 10, block A, XXX building, No. XXX Road, XXX district, XXX City]> </Addr> <OffPostCode> <! [CDATA [100086]> </OffPostCode> </Information> </Informations> </Company> <Position> <ReleaseDate> <! [CDATA [2011-04-14]> </ReleaseDate> <Description> <! [CDATA [Responsibilities: 1. Responsible for routine maintenance and troubleshooting of office equipment such as copiers in Hangzhou, to maintain the relationship between customers and customers; 2, sales of office consumables, such as toner cartridges and paper, during maintenance or repair for customers in the responsible area. Job requirements: 1. Technical secondary school or above, major in computer or electronics; 2. Familiar with Hangzhou terrain, able to endure hardships and be motivated; 3. Good hands-on experience, preferred. Salary and benefits: base salary + Commission + work meals + Five insurances and one gold + high temperature subsidies + paid training + free health check + day fee, etc.]> </Description> <PositionName> <! [CDATA [photocopier Repair Engineer]> </PositionName> <Number> <! [CDATA [5]> </Number> <Education> <! [CDATA [Technical Secondary School]> </Education> <Salary> <! [CDATA [2000-2999]> </Salary> <Citys> <City> <! [CDATA [Hangzhou]> </City> </Citys> <Emails> <Email> <! [CDATA [zhouqiaoyan@techhero.com.cn]> </Email> </Emails> <URLFrom> http://search.51job.com/job/45205821,c.html </URLFrom> <SnapshoAddr> snapshots </SnapshoAddr> </Position> </Positions> snapshots -------------------------------------------------------------------- 2. test01.aspx. the cs code is as follows: -------------------------------------------------------------- using System; using System. collec Tions; using System. configuration; using System. data; using System. linq; using System. web; using System. web. security; using System. web. UI; using System. web. UI. htmlControls; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. xml. linq; using System. collections. generic; using System. xml; namespace Test. xml {public partial class test01: System. web. UI. page {protected void Pag E_Load (object sender, EventArgs e) {if (! IsPostBack) {DateTime begin = System. dateTime. now; TimeSpan span = begin-begin; Positions obj = new Positions (); int num = 10000; for (int I = 0; I <num; I ++) {this. makeData (Server. mapPath ("~ /Xml/work. xml "), ref obj); obj. dispose ();} DateTime end = DateTime. now; span = end-begin; this. page. response. write (string. format ("span = {0} <br/> begin = {1} <br/> end = {2} <br/>", span. toString (), begin. toString ("yyyy/MM/dd HH: mm: ss: fff"), end. toString ("yyyy/MM/dd HH: mm: ss: fff ")));}} /// <summary> /// main function for processing xml data /// 1. xml files can be processed. For details, see xdoc. load (string xmlPath) // 2. xml strings can be processed. For details, see xdoc. load (string XmlInfo) // adjust the function parameters and comment out the code as needed // </summary> private void MakeData (string fpath, ref Positions obj) {string FPA = Server. mapPath ("~ /Xml/work. xml "); // xdoc. loadXml (strXml); System. xml. xmlDocument xdoc = null; try {xdoc = new System. xml. xmlDocument (); xdoc. load (FPA); if (xdoc. childNodes. count> 0) {obj = new Positions (); foreach (XmlNode node in xdoc. childNodes [0]. childNodes) {this. makePoints (node, obj) ;}} catch (Exception ex) {//} finally {xdoc = null ;}} /// <summary> /// process the subnodes and attributes of the node based on the xml borrow point, save the data to the custom object /// </summary> /// <param name = "node"> </param> /// <param name = "obj "> </param> private void MakePoints (XmlNode node, positions obj) {string name = node. name. trim (); switch (name) {case "Company": {this. fun_Company (node, obj); break;} case "Position": {this. fun_Position (node, obj); break; }}/// <summary> // Company big node parses and processes data, and save the data to the custom object Company /// </summary> /// <param name = "node"> </param> /// <param name = "obj"> </param> protected void Fun_Company (XmlNode node, positions obj) {obj. myCompany = new Company {CorpName = node. selectSingleNode ("descendant: CorpName "). innerText, OfficeAddr1 = this. fun_Single (node, "OfficeAddr1", obj), OffPostCode1 = this. fun_Single (node, "OffPostCode1", obj), CompanySize = this. fun_Single (node, "CompanySize", obj), AboutUs = this. fun_Single (node, "AboutUs", obj),}; this. fun_Multi (node. selectSingleNode ("descendant: TradeCodes"), obj); this. fun_Multi (node. selectSingleNode ("descendant: Websites"), obj); this. fun_Multi (node. selectSingleNode ("descendant: Informations"), obj) ;}/// <summary> // Position big node parses and processes data, and save the data to the custom object Position /// </summary> /// <param name = "node"> </param> /// <param name = "obj"> </param> private void Fun_Position (XmlNode node, positions obj) {obj. myPosition = new Position {ReleaseDate = this. fun_Single (node, "ReleaseDate", obj), Description = this. fun_Single (node, "Description", obj), PositionName = this. fun_Single (node, "PositionName", obj), Number = this. fun_Single (node, "Number", obj), Education = this. fun_Single (node, "Education", obj), Salary = this. fun_Single (node, "Salary", obj), URLFrom = this. fun_Single (node, "URLFrom", obj), SnapshoAddr = this. fun_Single (node, "SnapshoAddr", obj),}; this. fun_Multi (node. selectSingleNode ("descendant: Citys"), obj); this. fun_Multi (node. selectSingleNode ("descendant: Emails"), obj);} // <summary> // process xml nodes without subnodes, and keep all data to the custom object // </summary> /// <param name = "node"> </param> /// <param name = "child"> </param> /// <param name = "obj"> </param> /// <returns> </returns> private string Fun_Single (XmlNode node, string child, Positions obj) {string resutl = ""; resutl = node. selectSingleNode (string. format ("descendant ::{ 0}", child )). innerText; return resutl;} // <summary> // process xml nodes with several subnodes, and keep all data to the custom object // </summary> /// <param name = "node"> </param> /// <param name = "obj"> </param> private void Fun_Multi (XmlNode node, positions obj) {switch (node. name) {case "TradeCodes": {foreach (XmlNode I in node. childNodes) {obj. myCompany. tradeCodes. add (I. innerText);} break;} case "Informations": {foreach (XmlNode I in node. childNodes) {Dictionary <string, string> item = new Dictionary <string, string> (); obj. myCompany. informations. add (item); foreach (XmlNode j in I. childNodes) {item. add (j. name, j. innerText) ;}} break;} case "Websites": {foreach (XmlNode I in node. childNodes) {obj. myCompany. websites. add (I. innerText);} break;} case "Citys": {foreach (XmlNode I in node. childNodes) {obj. myPosition. citys. add (I. innerText);} break;} case "Emails": {foreach (XmlNode I in node. childNodes) {obj. myPosition. emails. add (I. innerText) ;}break ;}}/// <summary >/// class defined based on Xml properties /// </summary> public class Positions: baseClass {public Positions () {this. myCompany = new Company (); this. myPosition = new Position ();} public Company MyCompany {get; set;} public Position MyPosition {get; set ;}} public class Company: BaseClass {public Company () {this. tradeCodes = new List <string> (); this. websites = new List <string> (); this. informations = new List <Dictionary <string, string> () ;}public string CorpName {get; set;} public string OfficeAddr1 {get; set;} public string OffPostCode1 {get; set;} public string CompanySize {get; set;} public List <string> TradeCodes {get; set;} public string AboutUs {get; set ;} public List <string> Websites {get; set;} public List <Dictionary <string, string> Informations {get; set ;}} public class Position: BaseClass {public Position () {this. citys = new List <string> (); this. emails = new List <string> ();} public string ReleaseDate {get; set;} public string Description {get; set;} public string PositionName {get; set ;} public string Number {get; set;} public string Education {get; set;} public string Salary {get; set;} public List <string> Citys {get; set ;} public List <string> Emails {get; set;} public string URLFrom {get; set;} public string SnapshoAddr {get; set ;}} public class BaseClass: IDisposable {public void Dispose (){}}}

3. Test results: 1000 transactions/0.3 seconds

[Download Code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.