Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a XML file loaded:

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

How do you add schema info for each "Table" data?
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>

LINQ
C#
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")
                };


What I have tried:

Above needed schema INFO.

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>
Posted
Updated 30-May-16 14:39pm
Comments
Sergey Alexandrovich Kryukov 30-May-16 20:36pm    
Not clear. Do you have the schema already and need to validate the files against that schema. Where do you want to validate it? With C#?

Or do you need to develop the schema yourself? Do you need to modify the XML files to reference the schema, or you want to validate the unchanged files externally against that schema?

—SA

1 solution

XML Schemas are either DTD or XSD.
Simpliest explanation available; W3 XSD How To[^]
MSDN Example available; MSDN Sample XSD File[^]

Kind Regards
 
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