-1

I'm new with xml (c# net mvc ) and I have a problem to read data from Xml file. that I have complex type of data this is my xml file:

<Contour>
 <Elements>  
  <Element Type="Point" Indice="859">Route marseille<Limites>
  <Haut>26.5</Haut>
  <Bas>43.2</Bas>
  </Limites>
  <Point id="01"    X="45"  Y="44" >12</Point>
  <Point id="02"    X="5"   Y="41" >5</Point>
  <Point id="03"    X="4"   Y="464" >3</Point>
  </Element>
 <Element Type="Point" Indice="256">Route Bordeaux<Limites>
  <Haut>16.5</Haut>
  <Bas>12.2</Bas>
  </Limites>
  <Point id="05"    X="6.5" Y="22" >5</Point>
  <Point id="06"    X="58"  Y="46.5">5</Point>
  <Point id="07"    X="98"  Y="4.5" >6</Point>
  </Element>
  </Elements>
  </Contour>
  • https://stackoverflow.com/q/55828/1531971 Start writing some code... –  Jul 06 '18 at 18:25
  • Possible duplicate of [How does one parse XML files?](https://stackoverflow.com/questions/55828/how-does-one-parse-xml-files) –  Jul 06 '18 at 18:25

2 Answers2

1

You can use XmlDocument to read an XML from string or from file.

var xmlDoc = new XmlDocument();
xmlDoc.Load(@"c:\contour.xml");

or

xmlDoc.LoadXml("<Contour>....</Contour>");

then find a node with:

var node = xmlDoc.DocumentElement.SelectSingleNode("/Elements/Element");

read an attributte of the node:

string type = node.Attributes["Type"]?.InnerText;

read the text of the node:

string text = node.InnerText;

Here's a link to more complete answer

and a microsoft guide

Jorge
  • 57
  • 2
  • 7
0

Using Xml Linq

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);

            List<Element> elements = doc.Descendants("Element").Select(x => new Element() {
                type = (string)x.Attribute("Type"),
                indice = (int)x.Attribute("Indice"),
                name = x.FirstNode.ToString(),
                haut = (decimal)x.Descendants ("Haut").FirstOrDefault(),
                bas = (decimal)x.Descendants("Bas").FirstOrDefault(),
                points = x.Elements("Point").Select(y => new Point() {
                    id = (string)y.Attribute("id"),
                    x = (decimal)y.Attribute("X"),
                    y = (decimal)y.Attribute("Y"),
                    value = (int)y
                }).ToList()
            }).ToList();
        }
    }
    public class Element
    {
        public string type { get; set; }
        public int indice { get; set; }
        public string name { get; set; }
        public decimal haut { get; set; }
        public decimal bas { get; set; }
        public List<Point> points { get; set;}
    }
    public class Point
    {
        public string id { get; set; }
        public decimal x { get; set; }
        public decimal y { get; set; }
        public int value { get; set; }
    }
}

Here is results using Xml putting into a DataTable

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Type", typeof(string));
            dt.Columns.Add("Indice", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Haut", typeof(decimal));
            dt.Columns.Add("Bas", typeof(decimal));
            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("X", typeof(decimal));
            dt.Columns.Add("Y", typeof(decimal));
            dt.Columns.Add("Value", typeof(int));

            XDocument doc = XDocument.Load(FILENAME);

            foreach(XElement element in doc.Descendants("Element"))
            {
                string type = (string)element.Attribute("Type");
                int indice = (int)element.Attribute("Indice");
                string name = element.FirstNode.ToString();
                decimal haut = (decimal)element.Descendants("Haut").FirstOrDefault();
                decimal bas = (decimal)element.Descendants("Bas").FirstOrDefault();

                foreach(XElement point in element.Elements("Point"))
                {
                    string id = (string)point.Attribute("id");
                    decimal x = (decimal)point.Attribute("X");
                    decimal y = (decimal)point.Attribute("Y");
                    int value = (int)point;

                    dt.Rows.Add(new object[] { type, indice, name, haut, bas, id, x, y, value});
                }
            }
        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20