Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I read a XML file (XMLfile.xml) previously compiled in the root of a ASP.net program.

C#
           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")
                };
Int32[] num = new Int32[110000];
            string[] nam = new string[110000];
            double[] a1 = new double[110000];
            double[] ecc = new double[110000];
            double[] i = new double[110000];
            double[] N = new double[110000];
            double[] w = new double[110000];
            double[] Pyrs = new double[110000];
            double[] mm = new double[110000];
            double[] MA0 = new double[110000];

            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;
           }
XML
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <Number>1</Number>
    <Name>Ceres</Name>
    <a>2.7681117</a>
    <e>0.0757544</e>
    10.59166
    <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>
    34.84095
    <N>173.0882785</N>
    <w>309.98943</w>
    <Pyrs>4.62</Pyrs>
    <mm>0.2135153</mm>
    <MA0>163.60434</MA0>
  </Table>
...
</NewDataSet>


What I have tried:

This method works if you ant to display all of the data. I was to read the individual data!

DataSet dsread = new DataSet();
DataTable dtread = new DataTable();
dsread.ReadXml(Server.MapPath("XMLfile.xml"));
dtread = dsread.Tables[0];
Posted
Updated 31-May-16 20:40pm
v2
Comments
an0ther1 31-May-16 21:10pm    
What are you actually trying to do? You posted another question with the same data asking about schema's for an XML file.
If you require specific elements from the XML click the Improve Question link and advise the elements that you require.
A XML document can be traversed to get specific nodes, elements, attributes &/or values using XPath which is what I would assume you require.

Kind Regards
Sergey Alexandrovich Kryukov 31-May-16 21:35pm    
Not clear. What do you mean my "XML compilation"? What does it mean, "compiled in the root of an ASP.NET program"? What do you mean by "ASP.NET program"? Do you understand that HTTP is stateless protocol? Do you understand how the stateless nature of the protocol is related to your issue? (If not, do you know the Web application lifetime?)

If you can read XML, what's your problem?

—SA

1 solution

Hi,

Assuming that you are trying to read a pre-existing xml file and pick up a certain element, you can make use of XMLDocument object as shown below:

C#
using System.Xml;

string filePath = "D:\xmlFile.xml"; // give full path here
XMLDocument document = new XMLDocument();
string name = string.empty;
document.Load(filePath);

XMLNodeList nodes = document.SelectNodes("/NewDataSet/Table");

foreach(int nodeCount = 0; nodeCount< nodes.Count; nodeCount++)
{
    name = nodes[nodeCount]["name"].InnerText;
}


Please let me know if this works for you or not.

Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900