Parsing XML file using C#?

前端 未结 3 1320
故里飘歌
故里飘歌 2021-02-04 10:06

I\'m new to both XML and C#; I\'m trying to find a way to efficiently parse a given xml file to retrieve relevant numerical values, base on the \"proj_title\" value=heat_run or

3条回答
  •  迷失自我
    2021-02-04 10:29

    8MB really isn't very large at all by modern standards. Personally I'd use LINQ to XML:

    XDocument doc = XDocument.Load("ex.xml");
    var projects = doc.Descendants("proj_title")
                      .Where(x => (string) x == "heat_run")
                      .Select(x => x.Parent) // Just for simplicity
                      .Select(x => new {
                                  Start = (int) x.Element("proj_start"),
                                  End = (int) x.Element("proj_end")
                              });
    
    foreach (var project in projects)
    {
        Console.WriteLine("Start: {0}; End: {1}", project.Start, project.End);
    }
    

    (Obviously adjust this to your own requirements - it's not really clear what you need to do based on the question.)

    Alternative query:

    var projects = doc.Descendants("proj")
                      .Where(x => (string) x.Element("proj_title") == "heat_run")
                      .Select(x => new {
                                  Start = (int) x.Element("proj_start"),
                                  End = (int) x.Element("proj_end")
                              });
    

提交回复
热议问题