我是xml(c#net mvc)的新手,我无法从Xml文件读取数据。我有复杂的数据类型,这是我的xml文件:
<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>
答案 0 :(得分:1)
您可以使用XmlDocument从字符串或文件中读取XML。
xmlDoc.LoadXml("<Contour>....</Contour>");
或
var node = xmlDoc.DocumentElement.SelectSingleNode("/Elements/Element");
然后找到具有以下内容的节点:
string type = node.Attributes["Type"]?.InnerText;
读取节点的属性:
string text = node.InnerText;
读取节点的文本:
$ gem dependency mysql2 --reverse-dependencies
这里是指向更完整的http://homepage.tudelft.nl/h05k3/Projects/MeshThickeningProj.htm
的链接答案 1 :(得分:0)
使用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; }
}
}
这是使用Xml放入数据表的结果
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});
}
}
}
}
}