简体   繁体   中英

Read a XML file using LINQ

How do I read a XML file (XMLfile.xml) using LINQ in a previously made file in the root of a ASP.net program. Each Element exist in the XML file I have already created (see below for an excerpt).

        XDocument xmlElements = XDocument.Parse(System.IO.File.ReadAllText(Server.MapPath("XMLfile.xml")));

        var elements = from data in xmlElements.Descendants("/NewDataSet/Table")
            select new
            {
                Number0 = (int)data.Element("Number"),
                Name0 = (string)data.Element("Name"),
                a0 = (double)data.Element("a"),
                e0 = (double)data.Element("e"),
                i0 = (double)data.Element("i"),
                N0 = (double)data.Element("N"),
                w0 = (double)data.Element("w"),
                Pyrs0 = (double)data.Element("Pyrs"),
                mm0 = (double)data.Element("mm"),
                MA0 = (double)data.Element("MA0")
            };

        foreach (var element in elements)
        {
            m = m + 1;
            num[m] = element.Number0;
            nam[m] = element.Name0;
            a1[m] = element.a0;
            ecc[m] = element.e0;
            i[m] = element.i0;
            N[m] = element.N0;
            w[m] = element.w0;
            Pyrs[m] = element.Pyrs0;
            mm[m] = element.mm0;
            MA0[m] = element.MA0;
       }

XMLfile.xml

 <?xml version="1.0" standalone="yes"?>
 <NewDataSet>
    <Table>
       <Number>1</Number>
       <Name>Ceres</Name>
       <a>2.7681117</a>
       <e>0.0757544</e>
       <i>10.59166</I>
       <N>80.3218024</N>
       <w>72.73324</w>
       <Pyrs>4.61</Pyrs>
       <mm>0.2140072</mm>
       <MA0>181.38143</MA0>
    </Table>
    <Table>
       <Number>2</Number>
       <Name>Pallas</Name>
       <a>2.7723622</a>
       <e>0.2310236</e>
       <i>34.84095</i>
       <N>173.0882785</N>
       <w>309.98943</w>
       <Pyrs>4.62</Pyrs>
       <mm>0.2135153</mm>
       <MA0>163.60434</MA0>
    </Table>
    ...
  </NewDataSet>

What has worked for me is the extension method XPathSelectElements . As in:

var elements = from data in xmlElements.XPathSelectElements("/NewDataSet/Table")
    select new
    {
        Number0 = (int)data.Element("Number"),
        Name0 = (string)data.Element("Name"),
        a0 = (double)data.Element("a"),
        e0 = (double)data.Element("e"),
        i0 = (double)data.Element("i"),
        N0 = (double)data.Element("N"),
        w0 = (double)data.Element("w"),
        Pyrs0 = (double)data.Element("Pyrs"),
        mm0 = (double)data.Element("mm"),
        MA0 = (double)data.Element("MA0")
    };

The code below is XML Linq like you requests. XML is case sensitive so you need to change the closing tag "I" to "i". I tested the code below and it works. If you have a string instead of the file then change "Load" to "Parse".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("Table").Select(x => new
            {
                number = (int)x.Element("Number"),
                name = (string)x.Element("Name"),
                a = (double)x.Element("a"),
                e = (double)x.Element("e"),
                i = (double)x.Element("i"),
                n = (double)x.Element("N"),
                w = (double)x.Element("w"),
                pyrs = (double)x.Element("Pyrs"),
                mm = (double)x.Element("mm"),
                ma0 = (double)x.Element("MA0")
            }).ToList();

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM